How to set a delegate for a single cell in a Qt element view?

Rather puzzled by this omission, but in the Qt QAbstractItemView class, you can set QAbstractItemDelegate (i.e., QItemDelegate or QStyledItemDelegate ) to the entire view, one row or one column, using the setItemDelegate* methods. In addition, an item delegate for a single cell can be requested using QAbstractItemView::itemDelegate(const QModelIndex&) along with a delegate for rows, columns. and the whole view. But there seems to be no way to set the delegate of an element to a separate cell. Am I missing something? For what reason should it be?

+6
source share
2 answers

No, you cannot set an element delegate for only one cell or one column, but you can easily set an element delegate for the whole widget and choose in which cell, column or row you want to use your own picture or something like that.

For instance,

 void WidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.column() == 1) { // ohh it my column // better do something creative } else // it just a common column. Live it in default way QItemDelegate::paint(painter, option, index); } 

You can find more information here.

+4
source

I would recommend reimplementing the createEditor function:

 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; } 
+2
source

All Articles