2014-09-11 97 views
2

我想創建一個小Filebrowser這裏描述,但是通過使用PyQt5:Youtube-Video-DescriptionPyQt5 Filebrowser信號插槽:QTreeView則連接到QColumnview

到目前爲止佈局工作正常。現在我想實現文件在左側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連接的重要線路。

回答

0

您需要設置列視圖根指數:

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