PySide6 QTreeView Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# The QTreeView class provides a default model/view 
# implementation of a tree view.
# For the model I'm using QFileSystemModel
# provided by Qt so no need for subclassing.

import sys

from PySide6.QtCore import QDir
from PySide6.QtWidgets import QFileSystemModel
from PySide6.QtWidgets import (QApplication,
    QWidget, QVBoxLayout, QTreeView)


class Window(QWidget):
    
    def __init__(self):

        super().__init__()
        
        layout = QVBoxLayout()
        self.setLayout(layout)
        
        # 1 - Create the tree view
        
        tree_view = QTreeView()
        
        # 2 - Create the model
        
        model = QFileSystemModel()
        
        # 3 - Set the mode as the tree view's model
        
        tree_view.setModel(model)
        
        # 4 - This line sets the tree view root
        #     In this case I set it to my home directory
        #     Any changes to files and directories within root
        #     will be reflected in the model.
        
        tree_view.setRootIndex(
            model.setRootPath(QDir.home().path()))
        
        layout.addWidget(tree_view)
        
        # It's a start of a rudimentary file manager
        # in a few lines of code!


if __name__ == '__main__':

    if not QApplication.instance():
        app = QApplication(sys.argv)
    else:
        app = QApplication.instance()

    main_window = Window()
    main_window.show()

    sys.exit(app.exec())