【问题标题】:PyQt5 Filebrowser Signal-Slot: connecting QTreeView to QColumnviewPyQt5 Filebrowser Signal-Slot:将 QTreeView 连接到 QColumnview
【发布时间】:2014-09-11 10:18:04
【问题描述】:

我想创建一个如此处所述的小文件浏览器,但使用 PyQt5:Youtube-Video-Description

到目前为止,布局工作正常。现在我想实现文件显示在左侧 QColumnView 上的功能。意思是QTreeView中被点击的文件夹的文件可以在右边的File-QColumnView中看到。我不明白如何创建正确的信号来为 QColumnView 的 QFileSystemModel 设置路径索引。此外,如果我只能显示 Folder-QTreeView 的一个(名称)列会更好。 这是浏览器的代码:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QTreeView, QFileSystemModel, QApplication, QColumnView, QDockWidget, QMainWindow, QTextEdit
from PyQt5.QtCore import QDir, Qt

rootpath = QDir.currentPath()

class Browser(QMainWindow):
    def __init__(self):
        super(Browser, self).__init__()
        self.createDockWindows()
        self.textEdit = QTextEdit()
        self.setCentralWidget(self.textEdit)
    def createDockWindows(self):
        dock = QDockWidget("Folders", self)
        dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
        #Code to Create FileView Colums and FolderTree
        self.FileView = QtWidgets.QColumnView()
        self.FileView.setGeometry(QtCore.QRect(240, 10, 291, 281))
        self.FolderTree = QtWidgets.QTreeView()
        self.FolderTree.setGeometry(QtCore.QRect(10, 10, 221, 281))
        FolderTree = self.FolderTree
        #FolderTree.hidecolumn(1),... ?? to show only name column

        #include FolderTree to a Dock at the left side
        dock.setWidget(FolderTree)
        self.addDockWidget(Qt.LeftDockWidgetArea, dock)
        #set the model and rootpath for filling the FolderTree from self.ui
        dirmodel = QFileSystemModel()
        #set filter to show only folders
        dirmodel.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs)
        dirmodel.setRootPath(rootpath)
        #filemodel and filter for only files on right side
        filemodel = QFileSystemModel()
        filemodel.setFilter(QDir.NoDotAndDotDot | QDir.Files)
        filemodel.setRootPath(rootpath)
        FolderView = self.FolderTree
        FolderView.setModel(dirmodel)
        FolderView.setRootIndex(dirmodel.index(rootpath))
        FileView = self.FileView
        FileView.setModel(filemodel)
        dock = QDockWidget("Files", self)
        dock.setWidget(FileView)
        self.addDockWidget(Qt.RightDockWidgetArea, dock)

        #important lines for the connection, which does not work    
        self.FolderTree.clicked['QModelIndex'].connect(self.setpathonclick)
    def setpathonclick(self):
        currentpathindex = self.FolderTree.currentIndex()
        self.FileView.setCurrentIndex(currentpathindex)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Browser()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

最后12行是FolderTree和FileOverView的onclick连接的重要行。

【问题讨论】:

    标签: python qt pyqt5 signals-slots qtreeview


    【解决方案1】:

    您需要在 column-view 上设置根索引:

        filemodel = QFileSystemModel()
        filemodel.setFilter(QDir.NoDotAndDotDot | QDir.Files)
        filemodel.setRootPath(rootpath)
        ...
        FileView = self.FileView
        FileView.setModel(filemodel)
        FileView.setRootIndex(filemodel.index(rootpath))
    

    然后在点击处理程序中或多或少做同样的事情:

        ...
        self.FolderTree.clicked.connect(self.setpathonclick)
    
    def setpathonclick(self, index):
        rootpath = self.FolderTree.model().filePath(index)
        filemodel = self.FileView.model()
        filemodel.setRootPath(rootpath)
        self.FileView.setRootIndex(filemodel.index(rootpath))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多