首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyQt5文件浏览器信号插槽:将QTreeView连接到QColumnview

PyQt5文件浏览器信号插槽:将QTreeView连接到QColumnview
EN

Stack Overflow用户
提问于 2014-09-11 18:18:05
回答 1查看 1.1K关注 0票数 2

我想像这里描述的那样创建一个小的文件浏览器,但是使用PyQt5:Youtube-Video-Description

到目前为止,布局运行良好。现在,我想实现文件显示在左侧QColumnView上的功能。这意味着在QTreeView中点击的文件夹的文件可以在右边的文件-QColumnView中看到。我不明白如何创建正确的信号来为QColumnView的QFileSystemModel设置pathindex。此外,如果我只能显示文件夹的一个(名称)列-QTreeView,那就更好了。下面是浏览器的代码:

代码语言:javascript
复制
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连接的重要行。

EN

回答 1

Stack Overflow用户

发布于 2014-09-15 01:47:01

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

代码语言:javascript
复制
    filemodel = QFileSystemModel()
    filemodel.setFilter(QDir.NoDotAndDotDot | QDir.Files)
    filemodel.setRootPath(rootpath)
    ...
    FileView = self.FileView
    FileView.setModel(filemodel)
    FileView.setRootIndex(filemodel.index(rootpath))

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

代码语言:javascript
复制
    ...
    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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25784865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档