Qt - Python - Adding icons to treewidgetitems

23 Views Asked by At

I am using QTreeWidget, where I would like to add icon to each item (for easier user use). I have tried all the known ways (at least to me), such as:

However, none of the proposed ways worked. Not even using custom jpg (in case themes are messed up). Extract of my code is below:

    print("add folder")
    text, ok = QtWidgets.QInputDialog.getText(self.app, "Add new folder","New folder name:")
    child = ProjectBrowserItem([text],ProjectBrowserItemType.FOLDER)  #this is QTreeWidgetItem
    child.setForeground(0,QtGui.QBrush((QtGui.QColor("#996633"))))    #this is just to see if I can modify QTreeWidgetItem properties
    icon = QtGui.QIcon("folder.jpg")                                  #this is my custom 32x32 pixel icon
    child.setIcon(0,icon)                                             #setting the icon
    print(child.icon(0))                                              #just to see whether icon exists
    item.addChild(child)                                              #adding new child to the parent item
    item.setExpanded(True)                                            #expand everything
    self.show()                                                       #show the tree

and my printout:

Icon adding printout

and my icon (if needed):

My Custom Icon

However, this icon does not appear on my QTreeWidget. The result is following:

Outcome

As you see, no icon is added to the item Folder.

Am I missing something? Is there some additional update/resizing needed?

FYI, adding icons to buttons doesnt work either.

Thanks!!

0

There are 0 best solutions below