Aug-09-2023, 07:25 PM
QTreeView with QFileSystemModel ?
import sys from PyQt5.QtWidgets import (QFileSystemModel, QWidget, QApplication, QHBoxLayout, QTreeView) from PyQt5.QtCore import QDir class Widget(QWidget): def __init__(self, *args, **kwargs): QWidget.__init__(self, *args, **kwargs) hlay = QHBoxLayout(self) self.treeview = QTreeView() self.listview = QTreeView() hlay.addWidget(self.treeview) hlay.addWidget(self.listview) path = QDir.rootPath() self.dirModel = QFileSystemModel() self.dirModel.setRootPath(QDir.rootPath()) self.dirModel.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs) self.fileModel = QFileSystemModel() self.fileModel.setFilter(QDir.NoDotAndDotDot | QDir.Files) self.treeview.setModel(self.dirModel) self.listview.setModel(self.fileModel) self.treeview.setRootIndex(self.dirModel.index(path)) self.listview.setRootIndex(self.fileModel.index(path)) self.treeview.clicked.connect(self.on_clicked) def on_clicked(self, index): path = self.dirModel.fileInfo(index).absoluteFilePath() self.listview.setRootIndex(self.fileModel.setRootPath(path)) if __name__ == '__main__': app = QApplication(sys.argv) w = Widget() w.setGeometry(50, 50, 800, 600) w.show() sys.exit(app.exec_())