How to create attribute for QStrandardItem?



  • Good evening! I am using QStandardItemModel for the QTreeView and want to set hidden attributes for the QStandardItems to distinguish them. Is there a good solution for that? thanks!



  • You can define your own "Qt::ItemDataRoles":/doc/qt-4.8/qt.html#ItemDataRole-enum and use data() and setData() methods of the items.

    For example:

    @
    enum MyItemDataRoles { MyIdRole = Qt::UserRole + 1, MyStringDataRole };

    QStandardItem *item = ...;

    item->setData(23, MyIdRole);
    int id = item->data(MyIdRole).toInt();

    item->setData("blubb", MyStringDataRole);
    qDebug() << item->data(MyStringDataRole).toString();
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.