0

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!!

  • If adding icon to buttons doesn't work either, then that's not an issue with QTableWidget. Are you running the script in its directory or are you calling it with its path? You are using a relative path for the icon, so in that case it wouldn't work, as relative paths are always relative to the working dir. Try using an absolute path and see if it works. – musicamante Nov 27 '22 at 13:12
  • Thanks for the comment! I tried both, relative and absolute path. I tried built-in themes as well. Nothing works. I am running in spyder – Aljaz Jelen Nov 27 '22 at 13:22
  • Try with a bmp or png image. If that works, you're probably running into [this issue](https://stackoverflow.com/q/56491169/984421). – ekhumoro Nov 27 '22 at 17:24
  • that worked!! thanks for reply would you be kind and write an answer, so i can accept to help others? Regards – Aljaz Jelen Nov 27 '22 at 19:21
  • What exactly worked? Can you now load jpg images? And please gives some details of the platform you're on and the programming environment you're using. – ekhumoro Nov 27 '22 at 20:05
  • Loading png worked. Icons are visible. I'm using Spyder with python3.9 and pyqt5. All together running on Windows. – Aljaz Jelen Nov 27 '22 at 20:31
  • 1
    Okay - I have linked this as a duplicate. Please follow the guidance given there to enable jpg support on your system. – ekhumoro Nov 27 '22 at 21:25

0 Answers0