First, you must build a model by yourself. Actually, it is to implement the abstract class q1_actitemmodel. There are many articles about this process, such as examples.
Add the following code to the data function of the TreeModel class:
If role = QtCore. Qt. DecorationRole:
If index. column ()> 0: # only display the icon in the first column, which can be adjusted as needed
Return None
Node = self. nodeFromIndex (index)
If node. parent () = self. rootItem: # The root node does not display the icon.
Return None
If node. childCount ()> 0: # No icon is displayed if a subnode exists.
Return None
If node is None:
Return None
Else:
Pixmap = qtgui. qpixmap (":/icon1.png ")
If not pixmap. isnull ():
Return qtgui. qicon ("icon1.png ")
Return none
Because the image file is added, You need to modify the project's resource file simpletreemodel. qrc and add the following line to the description of the icon1.png file in <qresource>:
<! Doctype RCC> <RCC version = "1.0">
<Qresource>
<File> default.txt </File>
<File alias = "icon1.png"> icon1.png </File>
</Qresource>
</RCC>
Put icon1.png in the working directory and compile it.
Pyrcc4.exe some. qrc-o someqrc. py (if it is python3, add the-py3 option)
To become simpletreemodel_rc.py
Run the program and you will see the child node of the tree on the left side of the TreeView with the icon.