如何在 QListWidget 中将项目列为组 [英] How to list items as groups in QListWidget

查看:47
本文介绍了如何在 QListWidget 中将项目列为组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以分组设置 QListWidget 项.

Is it possible to to set QListWidget items in groups.

例如,如果我将文件夹的内容添加到 listwidget.我想显示文件夹组中的所有文件夹和文件组中的文件.如下图所示.

For example if i am adding the content of a folder to listwidget. i want to show all folders in folders group and files in files group. Like in the image below.

有没有可能.

推荐答案

为每个组创建一个包含所有项目的列表,按任意属性对列表进行排序,并将项目组按所需顺序添加到列表小部件.可以在组之间或组前添加分隔符或标题,组可以得到不同的样式.

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) 

对于文件和文件夹,QTreeView/QFileSystemModel 可能是另一种解决方案.

for files and folders QTreeView/QFileSystemModel might be another solution.

以相应的方式使用 QTableWidget 项目可以按行分组:

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

这篇关于如何在 QListWidget 中将项目列为组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆