Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Adding a property to QTreeWidgetItem



  • Is is possible to add a property, I know that's not possible, but something similar at least to it to a QTreeWidgetItem. I want to be able to add a setting for each row, so I know how to display it.
    So I have this QTreeWidget which has a bunch of stuff in it and I want to make it display a column in either hex, decimal or octal depending on the choice from a QActionGroup when you right click it. My current solution would be to add a suffix to it but it would be nicer to have a property or something.



  • @vikke
    You add whatever "properties" you want by sub-classing QTreeWidgetItem and putting whatever member variables you want into that. Then you add created instances of your sub-class into the QTreeWidget. This principle applies in many places in Qt.


  • Lifetime Qt Champion

    Hi
    Just as a note.
    There is also UserRoles for such data.
    QTreeWidgetItem * item = new QTreeWidgetItem();
    item->setData(0,Qt::UserRole, true );
    item->setData(0,Qt::UserRole+1, 100 );



  • @mrjj thank you very much, that solved my problem, I made an enum for the role so now I can add new ones as needed, is there some good way to make them or is it just making your own custom enum


  • Lifetime Qt Champion

    @vikke
    Hi
    enums are fine as the only important thing is that they all have unique values
    and using an enum makes that easy.
    It also makes code easier to read as you can better guess what data is.


Log in to reply