displaying several treeviews one below the other

28 views Asked by At

I have a project that consists in displaying a datamodel using qtreeview from a folder that contains several files. I displayed a treeview of a single file but i couldn't display several treeviews one under the other of several files. here is my 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()

my start_window.py where I need to implement the choose folder function to reach the goal :

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()

I displayed one tree view of a single parsed file , but when I click on choose folder i want to display all the treeviews of all the files in this folder

1

There are 1 answers

0
mac On

make suitable changes as needed.

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_())