I would like to add an icon for each item in my QTreeView
. The problem is : I am not sublassing my QTreeView
from QStandartItemModel
but from QAbstractItemModel
so I don't have access to the set_icon
method.
Here is the function I've done :
def set_icon(self,column : int , icon :QIcon) :
if column < 0 or column >= len(self.item_data) :
return False
self.icon = icon
self.item_data[column] = self.icon
return True
I copied this method from the set_data method that is found in the treeitem.py, I thought it would work the same way.
Then, when I populate my QTreeView
, I wrote this line so it should add an icon to the second column of the line :
child.set_icon(1,QIcon(":/Icons/C:/Users/E40055301/Desktop/Icones/mail.svg").pixmap(QSize()))
I don't have any error message, but the Icon doesn't show, so I tried to write it in many ways but none is working.
You need to return the icon in the data()
method for the DecorationRole
.
def data(self, index: QModelIndex, role: int = None):
if not index.isValid():
return None
if role not in (0, 1, 2): # using the enum values instead of names
return None
item: TreeItem = self.get_item(index)
value = item.data(index.column())
if role == Qt.DisplayRole or role == Qt.EditRole:
return value
if role == Qt.DecorationRole:
if value == 1:
return QIcon('path_to_icon_1.jpg')
if value == 2:
return QIcon('path_to_icon_1.jpg')