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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
| import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QTreeView, QFileSystemModel
from PyQt5.QtCore import QModelIndex, QAbstractItemModel, QVariant, Qt
class MyTreeView(QTreeView):
pass
class TreeItem:
def __init__(self, parent=None, text=''):
self.parent = parent
self.text = text
self.children = []
###########
def __repr__(self):
return '<TreeItem(%s, %s)>' % (self.parent, self.text)
###########
@property
def children_count(self):
return len(self.children)
def append_children(self, item):
self.children.append(item)
class MyTreeModel(QAbstractItemModel):
_column_labels = ('')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._root = TreeItem(text="Root")
##############
root_item = self._root
for word in ('alpha', 'beta', 'gamma', 'epsilon', 'omega', 'delta', 'zet'):
item = TreeItem(root_item, word)
#print('treeitem:', item)
root_item.append_children(item)
root_item = item
##############
def flags(self, index):
return Qt.ItemIsEnabled
def headerData(self, section, orientation, role):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return self._column_labels[section]
else:
return QVariant()
def rowCount(self, parent):
print()
print('=== rowCount ===')
print('parent row:', parent.row(), 'col:', parent.column())
print('valid:', parent.isValid())
if not parent.isValid():
parent_item = self._root
else:
parent_item = parent.internalPointer()
children_count = parent_item.children_count
print('rows: %s' % children_count)
return children_count
def columnCount(self, parent):
return len(self._column_labels)
def index(self, row, column, parent):
print('')
print('=== index ===')
print('row:%s col:%s parent:%s' % (row, column, parent))
if not self.hasIndex(row, column, parent):
return QModelIndex()
if not parent.isValid():
parent_item = self._root
else:
parent_item = parent.internalPointer()
try:
child_item = parent_item.children[row]
print('index created')
return self.createIndex(row, column, child_item)
except IndexError:
return QModelIndex()
def parent(self, index):
print('')
print('=== index ===')
node = index.internalPointer()
if node is None:
return QModelIndex()
parent = node.parent
if parent is None:
return QModelIndex()
grandparent = parent.parent
if grandparent is None:
return QModelIndex()
row = grandparent.child.index(parent)
return self.createIndex(row, 0, parent)
def data(self, index, role):
print()
print('=== data ===')
print('row:', index.row(), 'col:', index.column())
if not index.isValid():
return QVariant()
if role == Qt.DisplayRole:
print('display data')
if index.column() == 0:
#return QVariant(index.internalPointer().text)
return '123'
else:
return QVariant()
else:
return QVariant()
app = QApplication(sys.argv)
#wnd = QWidget()
wnd = QMainWindow()
wnd.resize(500, 500)
wnd.move(300, 100)
wnd.show()
#tree_model = QFileSystemModel()
#tree_model.setRootPath('/')
tree_model = MyTreeModel()
tree_view = MyTreeView()
tree_view.setModel(tree_model)
wnd.setCentralWidget(tree_view)
sys.exit(app.exec_()) |