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
61
| # QListView presents items stored in a model.
# ie. it uses the QT model/view architecture
# Models contain data and views display it
# so one view can be used to display multiple models and vv.
# It is similar to QListWidget but should be more flexible
import os
import sys
from PySide6.QtGui import QStandardItemModel, QStandardItem
from PySide6.QtWidgets import (QApplication,
QWidget, QVBoxLayout, QListView)
# We need model classes now
class Window(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.setLayout(layout)
# 1 - Create the view
list_view = QListView()
# 2 - Create and populate the model
self.model = QStandardItemModel()
self.populate_model()
# 3 - Connect the model and the view
list_view.setModel(self.model)
layout.addWidget(list_view)
def populate_model(self):
# This incantation returns the contents
# of your home directory as a list:
files = os.listdir(os.path.expanduser('~'))
for f in files:
item = QStandardItem(f)
self.model.appendRow(item)
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())
|