Qt World Summit: Submit your Presentation

How to add decorations (i.e. an icon) to tree view items?

  • The QTreeView object supplies its own icons for expanding or collapsing the items. This I'd certainly like to keep. But if you take the "Project" view of QtCreator as an example, it should also be possible to add a special icon to each item in addition to the "+/-", or arrows, or whatever else you have (depending on the OS to indicate collapsed/expanded).

    What I would like to do is to add an additional icon for each item which is different, depending whether or not there are child items.

    I set the icon size of the tree view in the constructor of my widget which displays it. Also, in the model I supply to the tree view, I have added code to the model's data() function to return the appropriate icon if it receives a role which is Qt::DecorationRole. The icon is available in the resources compiled into the application.

    What more do I have to do?

  • Lifetime Qt Champion

    What you see in the QtCreator TreeView is the icon returned by Qt::DecorationRole.

  • @christian-ehrlicher : Thank you ... I needed to add just one line of code to the function which sets up the data in the tree model ... now it is working properly!

Log in to reply