如何在主窗口内的pyqt中创建文件夹视图 [英] how to create folder view in pyqt inside main window

查看:30
本文介绍了如何在主窗口内的pyqt中创建文件夹视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个文件夹查看器来查看特定路径的结构.这个文件夹视图应该看起来像 PyQT 中的树小部件,我知道文件对话框可以提供帮助,但我需要将它放在我的主窗口中.
我尝试使用 QTreeWidget 实现这一点,并使用递归函数在文件夹内循环,但这太慢了.因为它需要递归大量文件夹.这是正确的方法吗?或者有一个现成的qt解决方案可以解决这个问题.
检查下图.

I'm trying to implement a folder viewer to view the structure of a specific path. and this folder view should look like a the tree widget in PyQT , i know that the file dialog can help , but i need to have it inside my main window.
i tried to implement this using QTreeWidget and i used a recursed function to loop inside folders, but this is too slow. since it needs to recurse around huge number of folders. is this the right way to do it? or there is a ready qt solution for this problem.
Check the figure below.


推荐答案

使用模型和视图.

"""An example of how to use models and views in PyQt4.
Model/view documentation can be found at
http://doc.qt.nokia.com/latest/model-view-programming.html.
"""
import sys

from PyQt4.QtGui import (QApplication, QColumnView, QFileSystemModel,
                         QSplitter, QTreeView)
from PyQt4.QtCore import QDir, Qt

if __name__ == '__main__':
    app = QApplication(sys.argv)
    # Splitter to show 2 views in same widget easily.
    splitter = QSplitter()
    # The model.
    model = QFileSystemModel()
    # You can setRootPath to any path.
    model.setRootPath(QDir.rootPath())
    # List of views.
    views = []
    for ViewType in (QColumnView, QTreeView):
        # Create the view in the splitter.
        view = ViewType(splitter)
        # Set the model of the view.
        view.setModel(model)
        # Set the root index of the view as the user's home directory.
        view.setRootIndex(model.index(QDir.homePath()))
    # Show the splitter.
    splitter.show()
    # Maximize the splitter.
    splitter.setWindowState(Qt.WindowMaximized)
    # Start the main loop.
    sys.exit(app.exec_())

这篇关于如何在主窗口内的pyqt中创建文件夹视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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