2 回答
TA贡献1804条经验 获得超8个赞
您只需过滤与 QFileSystemModel 的 rootPath() 关联的 inde 的子项:
from PySide2 import QtCore, QtGui, QtWidgets
class SuffixDirProxyModel(QtCore.QSortFilterProxyModel):
def __init__(self, parent=None):
super().__init__(parent)
self._suffix = ""
def filterAcceptsRow(self, source_row, source_parent):
source_model = self.sourceModel()
if (
self._suffix
and isinstance(source_model, QtWidgets.QFileSystemModel)
and source_parent == source_model.index(source_model.rootPath())
):
index = source_model.index(source_row, 0, source_parent)
name = index.data(QtWidgets.QFileSystemModel.FileNameRole)
file_info = source_model.fileInfo(index)
return name.split(".")[-1] == self._suffix and file_info.isDir()
return True
@property
def suffix(self):
return self._suffix
@suffix.setter
def suffix(self, s):
self._suffix = s
self.invalidateFilter()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
model = QtWidgets.QFileSystemModel()
model.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Dirs)
path = # "f:/root_folder"
model.setRootPath(path)
proxy = SuffixDirProxyModel()
proxy.suffix = "asset"
proxy.setSourceModel(model)
w = QtWidgets.QListView()
w.setViewMode(QtWidgets.QListView.IconMode)
w.setModel(proxy)
w.setRootIndex(proxy.mapFromSource(model.index(path)))
w.show()
sys.exit(app.exec_())
添加回答
举报