Pyside QWidgetList项目分配多个图标

问题描述 投票:2回答:1

我知道如何为QWidgetList中的项目分配1个图标。是否可以分配超过1个图标?就像我附上的图像一样,我想在'a'中添加第二个图标(绿色图标),并能够在某些条件下将其删除。下面的代码只是我想要实现的一个例子

enter image description here

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

class MainWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.orange = QIcon('orangeIconPath')
        self.green = QIcon('greenIconPath')
        self.layout=QVBoxLayout()
        self.setLayout(self.layout)

        self.widget1=QWidget()
        self.layout.addWidget(self.widget1)

        self.layout1=QVBoxLayout()
        self.widget1.setLayout(self.layout1)

        self.layout1.addWidget(QLabel("First layout"))
        self.qlist = QListWidget()
        self.layout1.addWidget(self.qlist)

        items = ['a','b','c']
        self.qlist.addItems(items)

        #This is where I'm struggling when it's more than 1 icon
        #enter a condition to check against each item and assign icon/s to the item when condition is met

        self.show()
python icons pyside qlistwidget pyside2
1个回答
2
投票

不,不是用QListWidget,至少不是自己的。

不过,您有很多选择。

在这种情况下,我个人的偏好是使用QTreeWidgetQTreeWidget的行为非常类似于具有多列的QListWidget,因此您可以在这些列中添加图标。如果你不想要它的话,你需要摆弄一些来配置列宽并可能删除标题栏,但这并不是特别困难(如果需要,我很乐意提供额外的建议) 。您也可以将QTableWidget用于相同的目的,但需要更多配置才能使其像列表一样运行。

另一种选择是以编程方式生成带有多个图标的新图标,因此您只需设置一个图像。

显而易见的是,create a delegate的观点更为先进。我不会在这里详细介绍,因为它有很多工作,但它也给你最大的控制权。


作为旁注,我建议不要使用QListWidget / QTreeWidget / QTableWidget视图类。他们真的很容易接受,但是真的值得熟悉使用模型视图QListView / QTreeView / QTableView类。编写自己的QAbstractListModelQAbstractTableModel子类非常容易,或者你可以使用QStringListModel作为简单列表和QStandardItemModel作为两种类型视图类之间的垫脚石。

© www.soinside.com 2019 - 2024. All rights reserved.