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

标签 python file browser pyqt pyside

我目前正在尝试将某种文件浏览器/“资源管理器”实现到一个程序中……我正在使用 Python 和 PySide 以及 Qt-window-toolkit。差不多this youtube-video显示我最后想要的行为。但是,本教程使用 C++ 作为编程语言,我还无法从 C++ 示例中推断出正确的 Python 代码。

基本上,我的问题是让右栏(文件 View )显示在左栏(树型文件夹 View )中单击的文件夹的内容。

#!/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 函数...但是,这似乎不是正确的方法。所以我想知道,我必须做些什么才能让它发挥作用?

最佳答案

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

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

根据我在 C++ 中使用 Qt 的经验,我能够弄清楚这一点。如果您能弄清楚它是如何转换为 Python 的,那么 C++ 中 Qt 的文档真的非常好。我能够通过查看 QFileSystemModel documentation 来解决这个问题.

关于python - 使用 Qt-GUI 的 Python 程序中的简单文件浏览器/文件选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9251644/

相关文章:

jquery - iOS 上 <a href ="url"> 和 window.location = "url"有什么区别?

java - 如何通过代码(例如 java)将按键输入发送到浏览器

python - dask 数组上的 if 语句

python - 在 python 中缩写双重比较

Python Django 错误 ModuleNotFoundError : No module named 'corsheaders'

java - 如何从 FileDescriptor 创建文件?

java - FileUtils.iterateFiles 忽略

python - ZODB:transaction.savepoint 向哪里写入数据?

c - 延迟函数可以在 64 位编译器的 C 语言中工作吗?

android - 如何通过 Android 手机访问我的本地主机?