PDA

View Full Version : Retrieve QTreeWidgetItem in delegate



torres
4th April 2011, 20:02
I have a class that inherits QStyledItemDelegate. I override the following methods:



QWidget *createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
void setModelData( QWidget * editor, QAbstractItemModel * model, const QModelIndex & index ) const;
void setEditorData ( QWidget * editor, const QModelIndex & index ) const;


Is it possible to retrieve the QTreeWidgetItem on which the user clicked in the createEditor method of the delegate?

Thanks in advance

wysota
4th April 2011, 21:57
If you pass a pointer to the tree widget to the delegate so that you can retrieve it in createEditor() then you can use QTreeWidget::itemFromIndex().

Phlucious
3rd June 2016, 19:56
Sorry for resurrecting this, but it's a very useful post. How does this work, though, since QTreeWidget::itemFromIndex is protected?

Added after 22 minutes:

In the end, I didn't need the item but rather its data. Instead, I called QModelIndex::data() with the appropriate data role and worked with that. That's what QStyledItemDelegate does in the base implementation, by the way. Depending on your use case, that might work for you.