-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathTestJsonModel.py
More file actions
165 lines (141 loc) · 4.71 KB
/
TestJsonModel.py
File metadata and controls
165 lines (141 loc) · 4.71 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2025/08/05
@author: Irony
@site: https://pyqt.site | https://github.com/PyQt5
@email: 892768447@qq.com
@file: TestJsonModel.py
@description:
"""
import sys
from Lib.qjsonmodel import QJsonItem, QJsonModel
try:
from PyQt5.QtCore import QModelIndex, QSortFilterProxyModel, Qt
from PyQt5.QtWidgets import (
QAction,
QApplication,
QCheckBox,
QGridLayout,
QLineEdit,
QPlainTextEdit,
QTreeView,
QWidget,
)
except ImportError:
from PySide2.QtCore import QModelIndex, QSortFilterProxyModel, Qt
from PySide2.QtWidgets import (
QAction,
QApplication,
QCheckBox,
QGridLayout,
QLineEdit,
QPlainTextEdit,
QTreeView,
QWidget,
)
class TestWindow(QWidget):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.resize(800, 600)
layout = QGridLayout(self)
self.filterPath = QCheckBox("Path Filter?", self)
self.filterEdit = QLineEdit(self)
self.treeView = QTreeView(self)
self.widgetEdit = QPlainTextEdit(self)
layout.addWidget(self.filterPath, 0, 0, 1, 1)
layout.addWidget(self.filterEdit, 0, 1, 1, 1)
layout.addWidget(self.treeView, 1, 0, 1, 2)
layout.addWidget(self.widgetEdit, 0, 2, 2, 1)
self.filterPath.toggled.connect(self.onFilterPathToggled)
self.filterEdit.setPlaceholderText("过滤条件")
self.filterEdit.textChanged.connect(self.onFilterTextChanged)
self.model = QJsonModel(self)
self.model.itemChanged.connect(self.onItemChanged)
self.model.rowsRemoved.connect(self.onRowsRemoved)
self.treeView.clicked.connect(self.onItemChanged)
action = QAction("Delete", self.treeView)
action.triggered.connect(self.deleteItem)
self.treeView.setContextMenuPolicy(Qt.ActionsContextMenu)
self.treeView.addAction(action)
# setup model data
self.setupModel()
# filter model
self.fmodel = QSortFilterProxyModel(self)
self.fmodel.setSourceModel(self.model)
self.fmodel.setRecursiveFilteringEnabled(True)
self.treeView.setModel(self.fmodel)
self.treeView.expandAll()
def setupModel(self):
data = {
"name": "Irony",
"age": 33,
"address": {
"country": "China",
"city": "Chengdu",
},
"phone": [
{"type": "home", "number": "123456789"},
{"type": "fax", "number": "6987654321"},
],
"marriage": False,
"salary": 6666.6,
"skills": [
"C++",
"Python",
"Java",
"JavaScript",
"Shell",
"Android",
],
"others": [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
],
}
self.model.blockSignals(True)
self.model.loadData(data)
self.model.blockSignals(False)
self.treeView.expandAll()
self.doExport()
def doExport(self):
self.widgetEdit.setPlainText(self.model.toJson(ensure_ascii=False, indent=4))
def deleteItem(self, _):
indexes = self.treeView.selectedIndexes()
if not indexes:
return
index = indexes[0]
print(index.row(), index.column(), index.parent())
self.model.removeRow(index.row(), index.parent())
def onRowsRemoved(self, parent, first, last):
print("onRowsRemoved", parent, first, last)
self.doExport()
def onItemChanged(self, item):
if self.sender() == self.model:
self.doExport()
if isinstance(item, QModelIndex):
item = self.model.itemFromIndex(item)
if not item:
return
keyInfo = ""
if item.keyItem:
keyInfo = f"row={item.keyItem.row()}, col={item.keyItem.column()}, text={item.keyItem.text()}"
print(
f"row={item.row()}, col={item.column()}, text={item.text()}, value={str(item.data(Qt.EditRole))}, key=({keyInfo})\n\trowCount={item.rowCount()}\n\tpath={item.path}"
)
def onFilterPathToggled(self, checked):
if checked:
self.fmodel.setFilterRole(QJsonItem.PathRole)
else:
self.fmodel.setFilterRole(Qt.DisplayRole)
def onFilterTextChanged(self, text):
self.fmodel.setFilterFixedString(text)
if __name__ == "__main__":
import cgitb
import sys
cgitb.enable(format="text")
app = QApplication(sys.argv)
w = TestWindow()
w.show()
sys.exit(app.exec_())