在一个下方显示多个树视图

0 投票
1 回答
24 浏览
提问于 2025-04-12 15:34

我有一个项目,目的是用qtreeview来展示一个数据模型,这个模型来自一个包含多个文件的文件夹。我已经成功展示了一个文件的树形视图,但我还不能把多个文件的树形视图一个接一个地展示出来。

这是我的main.py:

import sys
from PyQt5.QtWidgets import *
from start_window import StartWindow

def main():
    app = QApplication(sys.argv)
    start_window = StartWindow()
    start_window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

这是我的start_window.py,我需要在这里实现选择文件夹的功能,以达到我的目标:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from table_view import Table
from odl_parser import ODLParser

class StartWindow(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('Open ODL File or Folder')
        self.resize(600, 400) 

        main_layout = QHBoxLayout(self)

        self.image_label = QLabel(self)
        self.image_label.setPixmap(QPixmap('ACLViewer.jpg').scaled(300, 300, Qt.KeepAspectRatio))
        main_layout.addWidget(self.image_label)

        buttons_layout = QVBoxLayout()

        self.choose_file_button = QPushButton('Choose File', self)
        self.choose_file_button.clicked.connect(self.choose_file)
        buttons_layout.addWidget(self.choose_file_button)
        
        self.choose_folder_button = QPushButton('Choose Folder', self)
        self.choose_folder_button.clicked.connect(self.choose_folder)
        buttons_layout.addWidget(self.choose_folder_button)

        main_layout.addLayout(buttons_layout)

    def choose_file(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "Open ODL File", "", "ODL Files (*.odl)")
        if file_path:
            self.open_table_view(file_path)

    def choose_folder(self):
        folder_path = QFileDialog.getExistingDirectory(self, "Open Folder Containing ODL Files")
        if folder_path:
            # code for the folder 

    def open_table_view(self, file_path):
        parser = ODLParser(file_path)
        window = Table(parser)
        window.setWindowTitle('DM Parsing - ' + file_path)
        window.show()

我展示了一个解析过的单个文件的树形视图,但当我点击选择文件夹时,我想展示这个文件夹中所有文件的树形视图

1 个回答

-2

根据需要进行适当的修改。

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTreeView, QFileSystemModel


class FileTreeView(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("File Treeview")
        self.setGeometry(100, 100, 800, 600)

        # Create a QTreeView widget
        self.tree_view = QTreeView(self)
        self.tree_view.setGeometry(50, 50, 700, 500)

        # Create a QFileSystemModel instance
        self.model = QFileSystemModel()
        self.model.setRootPath('')
        self.tree_view.setModel(self.model)

        # Set the root path for the model
        folder_path = "/path/to/your/folder"  # Change this to your folder path
        self.tree_view.setRootIndex(self.model.index(folder_path))

        # Expand the treeview to show all contents
        self.tree_view.expandAll()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    file_treeview = FileTreeView()
    file_treeview.show()
    sys.exit(app.exec_())

撰写回答