0

PyQt5 代码:

from PyQt5.QtWidgets import QApplication, QFileSystemModel, QTreeView
from pathlib import Path

app =QApplication([])

model = QFileSystemModel()
cwd_str = f'{Path.cwd()}'
model.setRootPath(cwd_str)

treeview = QTreeView()
treeview.setModel(model)


root_idx = model.index(cwd_str)
treeview.setRootIndex(root_idx)

treeview.show()
app.exec()

PyQt6 代码:

from PyQt6.QtGui import QFileSystemModel
from PyQt6.QtWidgets import QApplication, QTreeView
from pathlib import Path

app =QApplication([])

model = QFileSystemModel()
cwd_str = f'{Path.cwd()}'
model.setRootPath(cwd_str)

treeview = QTreeView()
treeview.setModel(model)

root_idx = model.index(cwd_str)
treeview.setRootIndex(root_idx)

treeview.show()
app.exec()

我已经明确尝试设置 QFileIconProvider :(来自PyQt QFileIconProvider 类自定义图标

class IconProvider(QtGui.QFileIconProvider):
    def icon(self, fileInfo):
        if fileInfo.isDir():
            return QtGui.QIcon("folder.png") 
        return QtGui.QFileIconProvider.icon(self, fileInfo)

对于 PyQt6,这将显示为目录提供的图标,但仍然不显示其他文件的图标

4

0 回答 0