1212
1313import sys
1414
15- from Lib .qjsonmodel import QJsonModel
15+ from Lib .qjsonmodel import QJsonItem , QJsonModel
1616
1717try :
18- from PyQt5 .QtCore import QModelIndex , Qt
18+ from PyQt5 .QtCore import QModelIndex , QSortFilterProxyModel , Qt
1919 from PyQt5 .QtWidgets import (
2020 QAction ,
2121 QApplication ,
22- QHBoxLayout ,
22+ QCheckBox ,
23+ QGridLayout ,
24+ QLineEdit ,
2325 QPlainTextEdit ,
2426 QTreeView ,
2527 QWidget ,
2628 )
2729except ImportError :
28- from PySide2 .QtCore import QModelIndex , Qt
30+ from PySide2 .QtCore import QModelIndex , QSortFilterProxyModel , Qt
2931 from PySide2 .QtWidgets import (
3032 QAction ,
3133 QApplication ,
32- QHBoxLayout ,
34+ QCheckBox ,
35+ QGridLayout ,
36+ QLineEdit ,
3337 QPlainTextEdit ,
3438 QTreeView ,
3539 QWidget ,
@@ -40,24 +44,39 @@ class TestWindow(QWidget):
4044 def __init__ (self , * args , ** kwargs ) -> None :
4145 super ().__init__ (* args , ** kwargs )
4246 self .resize (800 , 600 )
43- layout = QHBoxLayout (self )
47+ layout = QGridLayout (self )
48+ self .filterPath = QCheckBox ("Path Filter?" , self )
49+ self .filterEdit = QLineEdit (self )
4450 self .treeView = QTreeView (self )
4551 self .widgetEdit = QPlainTextEdit (self )
46- layout .addWidget (self .treeView )
47- layout .addWidget (self .widgetEdit )
52+ layout .addWidget (self .filterPath , 0 , 0 , 1 , 1 )
53+ layout .addWidget (self .filterEdit , 0 , 1 , 1 , 1 )
54+ layout .addWidget (self .treeView , 1 , 0 , 1 , 2 )
55+ layout .addWidget (self .widgetEdit , 0 , 2 , 2 , 1 )
56+
57+ self .filterPath .toggled .connect (self .onFilterPathToggled )
58+ self .filterEdit .setPlaceholderText ("过滤条件" )
59+ self .filterEdit .textChanged .connect (self .onFilterTextChanged )
4860
4961 self .model = QJsonModel (self )
5062 self .model .itemChanged .connect (self .onItemChanged )
5163 self .model .rowsRemoved .connect (self .onRowsRemoved )
52- self .treeView .setModel (self .model )
5364 self .treeView .clicked .connect (self .onItemChanged )
5465 action = QAction ("Delete" , self .treeView )
5566 action .triggered .connect (self .deleteItem )
5667 self .treeView .setContextMenuPolicy (Qt .ActionsContextMenu )
5768 self .treeView .addAction (action )
5869
70+ # setup model data
5971 self .setupModel ()
6072
73+ # filter model
74+ self .fmodel = QSortFilterProxyModel (self )
75+ self .fmodel .setSourceModel (self .model )
76+ self .fmodel .setRecursiveFilteringEnabled (True )
77+ self .treeView .setModel (self .fmodel )
78+ self .treeView .expandAll ()
79+
6180 def setupModel (self ):
6281 data = {
6382 "name" : "Irony" ,
@@ -127,6 +146,15 @@ def onItemChanged(self, item):
127146 f"row={ item .row ()} , col={ item .column ()} , text={ item .text ()} , value={ str (item .data (Qt .EditRole ))} , key=({ keyInfo } )\n \t rowCount={ item .rowCount ()} \n \t path={ item .path } "
128147 )
129148
149+ def onFilterPathToggled (self , checked ):
150+ if checked :
151+ self .fmodel .setFilterRole (QJsonItem .PathRole )
152+ else :
153+ self .fmodel .setFilterRole (Qt .DisplayRole )
154+
155+ def onFilterTextChanged (self , text ):
156+ self .fmodel .setFilterFixedString (text )
157+
130158
131159if __name__ == "__main__" :
132160 import cgitb
0 commit comments