使用Qt-GUI的Python程序中的简单文件浏览器/文件选择器? [英] Simple File browser / file chooser in Python program with Qt-GUI?

查看:650
本文介绍了使用Qt-GUI的Python程序中的简单文件浏览器/文件选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在程序中实现某种文件浏览器/资源管理器 ...我正在将Python和PySide与Qt-window-toolkit结合使用。 此youtube-视频或多或少显示了我最后想要的行为。但是,本教程使用C ++作为编程语言,但我仍无法从C ++示例中推断出正确的python代码。

I'm currently trying to implement some kind of file browser / "explorer" into a programme... I'm using Python and PySide in connection with the Qt-window-toolkit. More or less this youtube-video shows the behaviour I want to have at the end. However, this tutorial used C++ as programming language and I haven't been able yet to reason the right python code from the C++ example.

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

Basically, my problem is to get the right column (file view) showing the content of the folder clicked in the left column (tree-style folder 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 函数...但是,这并没有似乎是正确的。所以我想知道,要使其正常工作,我需要做什么?

As you can see in my code, I've already tried to use the setRootPath-function... however, that doesn't seem to be the correct one. So I wonder, what I've got to do for getting this to work?

推荐答案

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

You need to set the root index to the appropriate one in the file model. You can do this by adding the following line to the end of the clicked() function:

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

我能够从使用Qt的经验中找出来C ++。如果您能弄清楚如何将Qt转换为Python,那么C ++中的Qt的文档确实非常不错。通过查看 QFileSystemModel文档,我能够弄清楚这一点。

I was able to figure it out from my experience using Qt in C++. The documentation for Qt in C++ is really quite good if you can figure out how it translates to Python. I was able to figure this out by looking at the QFileSystemModel documentation.

这篇关于使用Qt-GUI的Python程序中的简单文件浏览器/文件选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆