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
#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


Messages In This Thread
RE: Hard disk structure like a file selection dialog - by Axel_Erfurt - Aug-09-2023, 07:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  DEC pack, unpack and disk-images Curbie 9 383 Yesterday, 07:12 AM
Last Post: Pedroski55
  [S0LVED] [Tkinter] Center dialog? Winfried 8 4,450 Mar-01-2022, 07:17 PM
Last Post: Winfried
  how to view all installed software in a dialog Tyrel 6 2,283 Oct-09-2021, 08:11 PM
Last Post: Tyrel
  Set Text in Open Dialog Box giddyhead 0 1,743 May-21-2021, 06:31 PM
Last Post: giddyhead
  Subprocesses not opening File Select Dialog teut 2 2,519 Feb-22-2021, 08:07 PM
Last Post: teut
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 8,020 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  File structure macfanpl 12 4,208 Sep-22-2020, 07:50 PM
Last Post: ndc85430
  How to Calculate CPU, Disk, Memory and Network utilization rate skvivekanand 1 2,115 Jun-16-2020, 08:53 PM
Last Post: jefsummers
  how to write offset number to disk use python? Pyguys 4 3,157 Apr-11-2020, 07:53 AM
Last Post: Pyguys
  File system representation in a data structure Alfalfa 1 2,141 Dec-18-2019, 01:56 AM
Last Post: Alfalfa

Forum Jump:

User Panel Messages

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