Pyqt display icons in qtreeview

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.