1. <i id='gSEKy'><tr id='gSEKy'><dt id='gSEKy'><q id='gSEKy'><span id='gSEKy'><b id='gSEKy'><form id='gSEKy'><ins id='gSEKy'></ins><ul id='gSEKy'></ul><sub id='gSEKy'></sub></form><legend id='gSEKy'></legend><bdo id='gSEKy'><pre id='gSEKy'><center id='gSEKy'></center></pre></bdo></b><th id='gSEKy'></th></span></q></dt></tr></i><div id='gSEKy'><tfoot id='gSEKy'></tfoot><dl id='gSEKy'><fieldset id='gSEKy'></fieldset></dl></div>

      <tfoot id='gSEKy'></tfoot>

      <small id='gSEKy'></small><noframes id='gSEKy'>

        <bdo id='gSEKy'></bdo><ul id='gSEKy'></ul>
    2. <legend id='gSEKy'><style id='gSEKy'><dir id='gSEKy'><q id='gSEKy'></q></dir></style></legend>

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

      时间:2023-08-05

        <tbody id='Ns7bw'></tbody>

        1. <small id='Ns7bw'></small><noframes id='Ns7bw'>

          <tfoot id='Ns7bw'></tfoot>

            <legend id='Ns7bw'><style id='Ns7bw'><dir id='Ns7bw'><q id='Ns7bw'></q></dir></style></legend>
            <i id='Ns7bw'><tr id='Ns7bw'><dt id='Ns7bw'><q id='Ns7bw'><span id='Ns7bw'><b id='Ns7bw'><form id='Ns7bw'><ins id='Ns7bw'></ins><ul id='Ns7bw'></ul><sub id='Ns7bw'></sub></form><legend id='Ns7bw'></legend><bdo id='Ns7bw'><pre id='Ns7bw'><center id='Ns7bw'></center></pre></bdo></b><th id='Ns7bw'></th></span></q></dt></tr></i><div id='Ns7bw'><tfoot id='Ns7bw'></tfoot><dl id='Ns7bw'><fieldset id='Ns7bw'></fieldset></dl></div>
            • <bdo id='Ns7bw'></bdo><ul id='Ns7bw'></ul>
              1. 本文介绍了如何在树视图中显示父目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

                In my application I have a QTreeview. I have a folder named "test" that contains many subfolders. The treeview only shows the subfolders not the test forlder it self!

                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 隐藏其他目录和文件.

                The rootIndex of the QTreeView is hidden so it is not shown. One possible solution is to pass the parent of the path and use a QSortFilterProxyModel to hide the other directories and files.

                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_())
                

                这篇关于如何在树视图中显示父目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:创建要嵌入到 QMainWindow 中的小部件 下一篇:如何渲染 QGraphicsScene 的一部分并将其保存为图像

                相关文章

                最新文章

                  • <bdo id='WKheR'></bdo><ul id='WKheR'></ul>
                  <legend id='WKheR'><style id='WKheR'><dir id='WKheR'><q id='WKheR'></q></dir></style></legend>

                  <i id='WKheR'><tr id='WKheR'><dt id='WKheR'><q id='WKheR'><span id='WKheR'><b id='WKheR'><form id='WKheR'><ins id='WKheR'></ins><ul id='WKheR'></ul><sub id='WKheR'></sub></form><legend id='WKheR'></legend><bdo id='WKheR'><pre id='WKheR'><center id='WKheR'></center></pre></bdo></b><th id='WKheR'></th></span></q></dt></tr></i><div id='WKheR'><tfoot id='WKheR'></tfoot><dl id='WKheR'><fieldset id='WKheR'></fieldset></dl></div>
                    <tfoot id='WKheR'></tfoot>
                  1. <small id='WKheR'></small><noframes id='WKheR'>