如何在树视图中显示父目录?

How to display parent directory in tree view?

在我的应用程序中,我有一个 QTreeview。我有一个名为 "test" 的文件夹,其中包含许多子文件夹。树视图只显示子文件夹而不是它自己的测试!

def create_treeview(self):
    self.treeView = QTreeView()
    self.treeView.setMinimumSize(QSize(250, 0))
    self.treeView.setMaximumSize(QSize(250, 16777215))
    self.treeView.setObjectName("treeView")

    self.dirModel = QFileSystemModel()
    self.dirModel.setRootPath(QDir.rootPath())
    self.dirModel.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs)

    self.treeView.setModel(self.dirModel)
    self.treeView.setRootIndex(self.dirModel.index("/home/data/test"))
    self.treeView.setHeaderHidden(True)
    self.treeView.clicked.connect(self.tree_click)
    return self.treeView

QTreeView 的rootIndex 被隐藏,所以没有显示。一种可能的解决方案是传递路径的父级并使用 QSortFilterProxyModel 隐藏其他目录和文件。

import os

from PyQt5.QtCore import pyqtSlot, QDir, QModelIndex, QSize, QSortFilterProxyModel
from PyQt5.QtWidgets import QApplication, QFileSystemModel, QMainWindow, QTreeView


class ProxyModel(QSortFilterProxyModel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._root_path = ""

    def filterAcceptsRow(self, source_row, source_parent):
        source_model = self.sourceModel()
        if self._root_path and isinstance(source_model, QFileSystemModel):
            root_index = source_model.index(self._root_path).parent()
            if root_index == source_parent:
                index = source_model.index(source_row, 0, source_parent)
                return index.data(QFileSystemModel.FilePathRole) == self._root_path
        return True

    @property
    def root_path(self):
        return self._root_path

    @root_path.setter
    def root_path(self, p):
        self._root_path = p
        self.invalidateFilter()


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.create_treeview()
        self.setCentralWidget(self.treeView)

    def create_treeview(self):

        path = "/home/data/test"

        self.treeView = QTreeView()
        self.treeView.setMinimumSize(QSize(250, 0))
        self.treeView.setMaximumSize(QSize(250, 16777215))
        self.treeView.setObjectName("treeView")

        self.dirModel = QFileSystemModel()
        self.dirModel.setRootPath(QDir.rootPath())
        self.dirModel.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs)

        root_index = self.dirModel.index(path).parent()

        self.proxy = ProxyModel(self.dirModel)
        self.proxy.setSourceModel(self.dirModel)
        self.proxy.root_path = path

        self.treeView.setModel(self.proxy)

        proxy_root_index = self.proxy.mapFromSource(root_index)
        self.treeView.setRootIndex(proxy_root_index)

        self.treeView.setHeaderHidden(True)
        self.treeView.clicked.connect(self.tree_click)

    @pyqtSlot(QModelIndex)
    def tree_click(self, index):
        ix = self.proxy.mapToSource(index)
        print(
            ix.data(QFileSystemModel.FilePathRole),
            ix.data(QFileSystemModel.FileNameRole),
        )


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())