How to list items as groups in QListWidget

僤鯓⒐⒋嵵緔 提交于 2019-12-07 04:55:27

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. Between or in front of the groups a delimiter or title can be added, the groups can get different styles.

e.g. 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.

using QTableWidget in a corresponding way items can be grouped in rows:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!