-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathTestModelModify.py
More file actions
167 lines (145 loc) · 4.62 KB
/
TestModelModify.py
File metadata and controls
167 lines (145 loc) · 4.62 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
166
167
#!/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: TestModelModify.py
@description:
"""
import json
import sys
from Lib.qjsonmodel import QJsonItem, QJsonModel
try:
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
QApplication,
QFormLayout,
QHBoxLayout,
QLineEdit,
QPlainTextEdit,
QPushButton,
QTreeView,
QWidget,
)
except ImportError:
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (
QApplication,
QFormLayout,
QHBoxLayout,
QLineEdit,
QPlainTextEdit,
QPushButton,
QTreeView,
QWidget,
)
class TestWindow(QWidget):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
layout = QHBoxLayout(self)
self.treeView = QTreeView(self)
self.widgetEdit = QWidget(self)
layout.addWidget(self.treeView)
layout.addWidget(self.widgetEdit)
layout = QFormLayout(self.widgetEdit)
layout.setFormAlignment(Qt.AlignLeft | Qt.AlignTop)
layout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow)
self.editPath = QLineEdit(f"address{QJsonItem.Sep}city", self.widgetEdit)
self.editPath.setPlaceholderText(
f"请输入查询内容,支持路径查询,比如:aaa{QJsonItem.Sep}bbb{QJsonItem.Sep}ccc"
)
layout.addRow("Path:", self.editPath)
self.editValue = QLineEdit("SiChuan", self.widgetEdit)
self.editValue.setPlaceholderText("请输入修改值,只支持路径匹配")
layout.addRow("Value:", self.editValue)
self.buttonQuery = QPushButton("查询", self.widgetEdit)
self.buttonModify = QPushButton("修改", self.widgetEdit)
self.buttonExport = QPushButton("获取Json", self.widgetEdit)
layout.addRow("", self.buttonQuery)
layout.addRow("", self.buttonModify)
layout.addRow("", self.buttonExport)
self.editText = QPlainTextEdit(self.widgetEdit)
self.editText.setReadOnly(True)
layout.addRow("", self.editText)
self.buttonQuery.clicked.connect(self.doQuery)
self.buttonModify.clicked.connect(self.doModify)
self.buttonExport.clicked.connect(self.doExport)
self.model = QJsonModel(self)
self.model.itemChanged.connect(self.onItemChanged)
self.treeView.setModel(self.model)
self.setupModel()
def doQuery(self):
self.editText.clear()
path = self.editPath.text().strip()
if not path:
return
item = self.model.findPath(path)
if item:
self.editText.appendPlainText(
str(
{
"path": item.path,
"text": item.text(),
"value": item.valueItem.edit,
}
)
)
def doModify(self):
self.editText.clear()
path = self.editPath.text().strip()
value = self.editValue.text().strip()
if not path or not value:
return
try:
value = json.loads(value)
except Exception:
pass
ret = self.model.updateValue(path, value)
self.editText.setPlainText(f"change ret: {ret}")
def doExport(self):
self.editText.setPlainText(self.model.toJson(ensure_ascii=False, indent=4))
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 onItemChanged(self, item):
self.doExport()
if __name__ == "__main__":
import cgitb
import sys
cgitb.enable(format="text")
app = QApplication(sys.argv)
w = TestWindow()
w.show()
sys.exit(app.exec_())