Python 如何显示QTreeView文件夹旁边的子文件夹数?

Python 如何显示QTreeView文件夹旁边的子文件夹数?,python,pyqt5,pyside2,Python,Pyqt5,Pyside2,我正在基于QFileSystemModel构建一个文件管理系统,需要在文件夹旁边显示子文件夹的数量。 首先,我需要知道孩子的数量 我知道行计数()不起作用。因为它仅在文件夹展开时计算 第二,我需要在文件夹名称后加上数字 我知道我应该向QFileSystemModel添加一个自定义列,但我不知道如何操作。因此,要获得行数,必须在调用fetchMore方法后执行该操作。以上所有内容都必须在委托中实现 导入系统 从PyQt5.QtCore导入QDir 从PyQt5.QtWidgets导入( QApp

我正在基于QFileSystemModel构建一个文件管理系统,需要在文件夹旁边显示子文件夹的数量。

首先,我需要知道孩子的数量

我知道行计数()不起作用。因为它仅在文件夹展开时计算

第二,我需要在文件夹名称后加上数字


我知道我应该向QFileSystemModel添加一个自定义列,但我不知道如何操作。

因此,要获得行数,必须在调用fetchMore方法后执行该操作。以上所有内容都必须在委托中实现

导入系统 从PyQt5.QtCore导入QDir 从PyQt5.QtWidgets导入( QApplication, QFileSystemModel, QStyledItemDelegate, QTreeView, ) 类StyledItemDelegate(QStyledItemDelegate): def initStyleOption(self、option、index): super().initStyleOption(选项,索引) 如果index.column()!=0: 返回 model=index.model() 如果model.hasChildren(索引): 如果model.canFetchMore(索引): model.fetchMore(索引) option.text+=“({})”格式(model.rowCount(index)) 如果名称=“\uuuuu main\uuuuuuuu”: app=QApplication.instance() 如果应用程序为无: app=QApplication(sys.argv) 视图=QTreeView() 查看。调整大小(640480) view.show() model=QFileSystemModel() model.setRootPath(QDir.currentPath()) view.setModel(model) view.setRootIndex(model.index(QDir.currentPath())) 委托=StyledItemDelegate(视图) view.setItemDelegate(委托) sys.exit(app.exec_())
我想补充一点,如果文本应该只指示子文件夹,就像OP最初请求的那样,
QDir(index.data(QFileSystemModel.FilePathRole),“”,filters=QDir.AllDirs | QDir.NoDotAndDotDot)。count()
可以代替
rowCount()
(可能有一些缓存,但这可能有点棘手,因为QFileSystemModel在fs更改通知方面存在一些问题)。