问题
I try to create a pyqt5 window with a Qtreeview window to load certain files.
I need to show multiples directories from different locations in the same Qtreeview as root folder, so it means that I need to have multiple roots (or faking it) in the same qtreeview
the directory structure can't be changed
I have read this thread but it does not fit my needs, as the number of folders to exclude will be enormous (more than 10 000 folders today) and will increase every days.
an 'include' function is more doable, but I don't found how to do it this way
here is my code for now, working for 'path1' :
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Widget(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)
path1 = "/mnt/volume1/somedirectory/root1" #QDir.rootPath()
path2 = '/mnt/volume2/root2'
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(path1))
self.listview.setRootIndex(self.fileModel.index(path1))
self.treeview.clicked.connect(self.on_clicked)
def on_clicked(self, index):
path1 = self.dirModel.fileInfo(index).absoluteFilePath()
self.listview.setRootIndex(self.fileModel.setRootPath(path1))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
My question is : how can I have 'path1' and 'path2' as roots (or gives the impression of it) without excluding manually a large amount of folders ?
来源:https://stackoverflow.com/questions/63657566/pyqt5-have-multiple-roots-qtreeview