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

6 投票
2 回答
15786 浏览
提问于 2025-04-17 12:54

我现在正在尝试在一个程序中实现某种文件浏览器或“资源管理器”的功能... 我使用的是Python和PySide,结合了Qt窗口工具包。大致上,这个YouTube视频展示了我最终想要的效果。不过,这个教程是用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 个回答

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

你需要在文件模型中设置正确的根索引。你可以通过在clicked()函数的最后添加以下一行来做到这一点:

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

我通过自己在C++中使用Qt的经验搞明白了这一点。如果你能理解Qt在Python中的用法,Qt的C++文档其实非常不错。我是通过查看QFileSystemModel的文档来弄清楚的。

撰写回答