How to double click to add directories and display in a blank space in Pyqt5?

Viewed 27

I want to write a directory administration gui, and I have code below:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class FormA(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        hlay = QHBoxLayout(self)
        self.treeview = QTreeView()
        self.listview = QListView()
        hlay.addWidget(self.treeview)
        hlay.addWidget(self.listview)

        add_btn = QVBoxLayout(self)
        self.ok_btn = QPushButton('Ok', self)
        hlay.addLayout(add_btn)
        add_btn.addWidget(self.ok_btn)


        self.setLayout(hlay)

        path = QDir.rootPath()

        self.dirModel = QFileSystemModel()
        self.dirModel.setRootPath(QDir.rootPath())
        self.dirModel.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs)
        self.treeview.setModel(self.dirModel)

        self.treeview.setRootIndex(self.dirModel.index(r'C:\Users\xx\admin'))

        self.treeview.clicked.connect(self.on_clicked)
    def on_clicked(self, index):
        path = self.dirModel.fileInfo(index).absoluteFilePath()
        print(path)

        
class FormB(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.btnPress = QPushButton("Table BBBB")
        layout = QVBoxLayout()
        self.setLayout(layout)
        layout.addWidget(self.btnPress)
        self.setStyleSheet("background-color:red;")

class TextEditDemo(QWidget):
    def __init__(self, parent=None):
        super(TextEditDemo, self).__init__(parent)
        self.setWindowTitle("Administration System")
        self.resize(800, 570)


        self.btnPress1 = QPushButton("Directories Admin")
        self.btnPress2 = QPushButton("Other functions")


        self.form1 = FormA()

        self.form2 = FormB()


        widget = QWidget()

        layout1 = QVBoxLayout()
        layout1.addWidget(self.btnPress1, alignment = Qt.AlignLeft)
        layout1.addWidget(self.btnPress2, alignment = Qt.AlignLeft)



        self.stacked_layout = QStackedLayout()
        widget.setLayout(self.stacked_layout)
        widget.setStyleSheet("background-color:white;")
        self.stacked_layout.addWidget(self.form1)
        self.stacked_layout.addWidget(self.form2)


        layout = QHBoxLayout()
        layout.addWidget(widget)

        layout.addLayout(layout1)





        self.setLayout(layout)



        self.btnPress1.clicked.connect(self.btnPress1_Clicked)
        self.btnPress2.clicked.connect(self.btnPress2_Clicked)


    def btnPress1_Clicked(self):
        self.stacked_layout.setCurrentIndex(0)


    def btnPress2_Clicked(self):
        self.stacked_layout.setCurrentIndex(1)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = TextEditDemo()
    win.show()
    sys.exit(app.exec_())


enter image description here Now I have 2 spaces, left one is QTreeView about listing the root directories, now I wanna add a function that if I double click the directory, it will be added to the right space, how do I implement it?

0 Answers
Related