Python Forum
Hard disk structure like a file selection dialog
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hard disk structure like a file selection dialog
#1
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?
Reply
#2
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_())
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [S0LVED] [Tkinter] Center dialog? Winfried 8 4,242 Mar-01-2022, 07:17 PM
Last Post: Winfried
  how to view all installed software in a dialog Tyrel 6 2,163 Oct-09-2021, 08:11 PM
Last Post: Tyrel
  Set Text in Open Dialog Box giddyhead 0 1,684 May-21-2021, 06:31 PM
Last Post: giddyhead
  Subprocesses not opening File Select Dialog teut 2 2,424 Feb-22-2021, 08:07 PM
Last Post: teut
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 7,820 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  File structure macfanpl 12 4,001 Sep-22-2020, 07:50 PM
Last Post: ndc85430
  How to Calculate CPU, Disk, Memory and Network utilization rate skvivekanand 1 2,059 Jun-16-2020, 08:53 PM
Last Post: jefsummers
  how to write offset number to disk use python? Pyguys 4 3,074 Apr-11-2020, 07:53 AM
Last Post: Pyguys
  File system representation in a data structure Alfalfa 1 2,080 Dec-18-2019, 01:56 AM
Last Post: Alfalfa
  Disk usage stats. MuntyScruntfundle 2 2,548 Jan-22-2019, 10:53 PM
Last Post: MuntyScruntfundle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020