PDA

View Full Version : MyCustomStandardItem and hidden data...



fruzzo
23rd May 2011, 11:06
Hi,
I'm trying to implement MyCustomStandardItem (inherited from QStandardItem) which has to contain some standard data (i.e. QString, QIcon, etc.) to visualize in a tree view and a list of events associated to the item (QList<Event>, where --> typedef QPair<QDataTime, int> Event) to not visualize in the same tree view...
Now...I want to visualize the list of events associated to each item through a tootip but I have some problem to retrieve the item events list starting from the item index...I try this solution:
MyCustomStandardItem* item = static_cast<MyCustomStandardItem*>(itemIndex.internalPointer());

item != NULL, but it crash just after a call an item member function.

yeye_olive
23rd May 2011, 12:43
That is because QModelIndex::internalPointer() "[r]eturns a void * pointer used by the model to associate the index with the internal data structure" and there is no way to tell what that internal structure is; in my opinion you should not be using this function. I suspect QStandardItemModel::itemFromIndex() might be what you are looking for.

Berryblue031
23rd May 2011, 14:56
Instead of creating MyCustomStandardItem I would recommend you use QStandardItem's built in mechanisms for custom data.


QStandardItem::setData ( const QVariant & value, int role = Qt::UserRole + 1 )

The data can then be retrieved with
QVariant QStandardItem::data ( int role = Qt::UserRole + 1 )

To draw the items properly in your widget create a custom QItemDelegate or QStyledItemDelegate

You also mention you are using a tree view. Consider QTreeWidget then you can use QTreeWidgetItemIterator to iterate over all of the items and extract data from them.

fruzzo
24th May 2011, 16:46
Instead of creating MyCustomStandardItem I would recommend you use QStandardItem's built in mechanisms for custom data.


QStandardItem::setData ( const QVariant & value, int role = Qt::UserRole + 1 )

The data can then be retrieved with
QVariant QStandardItem::data ( int role = Qt::UserRole + 1 )

To draw the items properly in your widget create a custom QItemDelegate or QStyledItemDelegate



Thanks...it's the solution that I chose!