TreeView and an Icon



  • I have a TreeView made with QAbstractItemModel. Now i want to add an Icon to the left of the Text. I found QStyledItemDelegate for that, but at the moment i dont know how to do that. Its very hard for a Beginner like me to understand that MV Framework in that way. I only want to show an Icon on the leftside of each TreeItem.

    Is it only possible to do that with the paint method in the QStyledItemDelegate? If yes, how?


  • Moderators

    @Fuel
    Is it only possible to do that with the paint method in the QStyledItemDelegate? If yes, how?

    if you have use QStandardItem you can use setIcon to set the icon but then then the actual drawing is done using drawPixmap . Something like this in paint:

    QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
    QPixmap pixmap = icon.pixmap(QSize(24, 24));
    painter->drawPixmap(rect(), pixmap);
    


  • Thats the Problem. I used an own Class for a TreeItem. Do i need to implement an Icon in to this Item Class?


  • Qt Champions 2016

    If you inherited QStandardItem for your class, it should still be the same?


Log in to reply
 

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