[英]How to list items as groups in QListWidget
Is it possible to to set QListWidget items in groups. 是否可以在组中设置QListWidget项。
For example if i am adding the content of a folder to listwidget. 例如,如果我将文件夹的内容添加到listwidget。 i want to show all folders in folders group and files in files group.
我想显示文件夹组中的所有文件夹和文件组中的文件。 Like in the image below.
如下图所示。
Is it possible. 可能吗。
Create for each group a list with all items, sort the list by an arbitrary property and add the item groups in desired order to listwidget. 为每个组创建包含所有项目的列表,按任意属性对列表进行排序,并按所需顺序将项目组添加到listwidget。 Between or in front of the groups a delimiter or title can be added, the groups can get different styles.
在组之间或组之前可以添加分隔符或标题,组可以获得不同的样式。
eg your files/folders groups: 例如你的文件/文件夹组:
files.sort() # list of files
folders.sort() # list of folders
for f in folders:
item = QtWidgets.QListWidgetItem()
item.setIcon(QtGui.QIcon('icon1.xpm'))
item.setText(f)
# set further properties like font, background ...
item.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEnabled)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem() # delimiter
item.setText('-----------------------')
item.setFlags(QtCore.Qt.NoItemFlags) # item should not be selectable
self.listWidget.addItem(item)
for f in files:
item = QtWidgets.QListWidgetItem()
item.setIcon(QtGui.Qicon('icon2.xpm'))
item.setText(f)
# set further properties like font, background ...
item.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEnabled)
self.listWidget.addItem(item)
for files and folders QTreeView/QFileSystemModel might be another solution. 对于文件和文件夹,QTreeView / QFileSystemModel可能是另一种解决方案。
using QTableWidget in a corresponding way items can be grouped in rows: 以相应的方式使用QTableWidget项可以按行分组:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.