使用Qt-GUI的Python程序中的简单文件浏览器/文件选择器?

时间:2012-02-12 18:55:19

标签: python file browser pyqt pyside

我目前正在尝试将某种文件浏览器/“资源管理器”实现到程序中...我正在使用Python和PySide与Qt-window-toolkit相关联。或多或少this youtube-video显示了我想要的结果。但是,本教程使用C ++作为编程语言,我还没有能够从C ++示例中推断出正确的python代码。

基本上,我的问题是获得正确的列(文件视图),显示左栏中单击的文件夹的内容(树状文件夹视图)。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from PySide import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.resize(600, 600)
        self.fileBrowserWidget = QtGui.QWidget(self)
        self.setCentralWidget(self.fileBrowserWidget)

        self.dirmodel = QtGui.QFileSystemModel()
        # Don't show files, just folders
        self.dirmodel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllDirs)
        self.folder_view = QtGui.QTreeView(parent=self);
        self.folder_view.setModel(self.dirmodel)
        self.folder_view.clicked[QtCore.QModelIndex].connect(self.clicked)

        # Don't show columns for size, file type, and last modified
        self.folder_view.setHeaderHidden(True)
        self.folder_view.hideColumn(1)
        self.folder_view.hideColumn(2)
        self.folder_view.hideColumn(3)

        self.selectionModel = self.folder_view.selectionModel()
        self.filemodel = QtGui.QFileSystemModel()
        # Don't show folders, just files
        self.filemodel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
        self.file_view = QtGui.QListView(parent=self);
        self.file_view.setModel(self.filemodel)

        splitter_filebrowser = QtGui.QSplitter()
        splitter_filebrowser.addWidget(self.folder_view)
        splitter_filebrowser.addWidget(self.file_view)
        splitter_filebrowser.setStretchFactor(0,2)
        splitter_filebrowser.setStretchFactor(1,4)

        hbox = QtGui.QHBoxLayout(self.fileBrowserWidget)
        hbox.addWidget(splitter_filebrowser)

    def set_path(self):
        self.dirmodel.setRootPath("")

    def clicked(self, index):
        # get selected path of folder_view
        index = self.selectionModel.currentIndex()
        dir_path = self.dirmodel.filePath(index)
        ###############################################
        # Here's my problem: How do I set the dir_path
        # for the file_view widget / the filemodel?
        ###############################################
        self.filemodel.setRootPath(dir_path)


app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
main.set_path()

sys.exit(app.exec_())

正如您在我的代码中所看到的,我已经尝试使用setRootPath - 函数...但是,这似乎不正确。所以我想知道,为了让它发挥作用我必须做些什么?

2 个答案:

答案 0 :(得分:5)

您需要将根索引设置为文件模型中的相应索引。您可以通过在clicked()函数的末尾添加以下行来完成此操作:

self.file_view.setRootIndex(self.filemodel.index(dir_path))

我能够从我在C ++中使用Qt的经验中找到答案。如果你能弄清楚它如何转换为Python,那么Qt在C ++中的文档真的非常好。我通过查看QFileSystemModel documentation来了解这一点。

答案 1 :(得分:4)

您需要设置文件列表视图的根索引:

def clicked(self, index):
    # the signal passes the index of the clicked item
    dir_path = self.filemodel.filePath(index)
    root_index = self.filemodel.setRootPath(dir_path)
    self.file_view.setRootIndex(root_index)