首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Qt-GUI的Python程序中的简单文件浏览器/文件选择器?

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

Stack Overflow用户
提问于 2012-02-13 02:55:19
回答 2查看 14.5K关注 0票数 5

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

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

代码语言:javascript
复制
#!/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-function...然而,这似乎不是正确的。所以我想知道,我该怎么做才能让它工作呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-13 03:37:52

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

代码语言:javascript
复制
self.file_view.setRootIndex(self.filemodel.index(dir_path))

我能够从我在C++中使用Qt的经验中弄清楚这一点。如果你能弄清楚如何将Qt翻译成C++,那么Qt的文档真的很不错。我能够通过查看QFileSystemModel documentation来弄清楚这一点。

票数 5
EN

Stack Overflow用户

发布于 2012-02-13 03:38:31

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

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

https://stackoverflow.com/questions/9251644

复制
相关文章

相似问题

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