Python Forum
Hard disk structure like a file selection dialog - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Hard disk structure like a file selection dialog (/thread-40512.html)



Hard disk structure like a file selection dialog - malonn - Aug-09-2023

I eventually want to build a tree of a drive contents structure just like a file/folder selection dialog (PyQt). I was thinking of building some sort of iterable first with the drive (and various things like file size) first to feed to the model I will be using. How would I create this iterable with file and folder structure intact? Is there a better approach. I know I can easily just recursively walk the drive, but that means (seems to me) there may be some unnecessary logic during tree building. That approach seems "wrong". I just don't know. Advice from more experienced coders?


RE: Hard disk structure like a file selection dialog - Axel_Erfurt - Aug-09-2023

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



RE: Hard disk structure like a file selection dialog - malonn - Aug-09-2023

Well, I've used a QFileSystemModel before and I don't like a couple of things about its functionality, so I wanted to write my own. Originally, I tried a subclassed QAbstractItemModel, but couldn't figure it out; beyond my skill level right now. So, I dropped back to my level and had complete success.

I'd share code for others, but it's split up between multiple modules. I'm lazy.

Thanks for your response though, @Axel_Erfurt.