在Qt的QAbstractItemView类中,可以使用setItemDelegate*
方法将QAbstractItemDelegate(即QItemDelegate或QStyledItemDelegate)设置为整个视图、单个行或单个列。此外,还可以使用QAbstractItemView::itemDelegate(const QModelIndex&)
查询单个单元格的项代理,以及行、列和整个视图的代理。但是似乎没有办法将项代理设置到单个单元格。我有什么遗漏吗?这应该有什么原因吗?
在Qt的QAbstractItemView类中,可以使用setItemDelegate*
方法将QAbstractItemDelegate(即QItemDelegate或QStyledItemDelegate)设置为整个视图、单个行或单个列。此外,还可以使用QAbstractItemView::itemDelegate(const QModelIndex&)
查询单个单元格的项代理,以及行、列和整个视图的代理。但是似乎没有办法将项代理设置到单个单元格。我有什么遗漏吗?这应该有什么原因吗?
不,您不能仅为一个单元格或一列设置项目委托,但是您可以轻松地为整个窗口小部件设置项目委托,并选择要在哪个单元格、列或行中使用自定义绘画或其他内容。
例如:
void WidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() == 1)
{
// ohh it's my column
// better do something creative
}
else // it's just a common column. Live it in default way
QItemDelegate::paint(painter, option, index);
}
You can find some more information here
我建议重新实现createEditor函数:
QWidget * WidgetDelegate::createEditor(
QWidget *parent,
const QStyleOptionViewItem &,
const QModelIndex &index) const
{
QWidget *widget = 0;
if (index.isValid() && index.column() < factories.size())
{
widget = factories[index.column()]->createEditor(index.data(Qt::EditRole).userType(), parent);
if (widget)
widget->setFocusPolicy(Qt::WheelFocus);
}
return widget;
}
void QAbstractItemView::setItemDelegateForColumn ( int column, QAbstractItemDelegate * delegate )
方法可以为单个列或单元格设置代理。 - Barath Ravikumar