forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 6
/
CityLinkage.py
115 lines (99 loc) · 4.13 KB
/
CityLinkage.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年1月27日
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@file: CityLinkage
@description: 下拉联动
"""
import json
import sys
import chardet
try:
from PyQt5.QtCore import Qt, QSortFilterProxyModel, QRegExp
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QWidget, QApplication, QHBoxLayout, QComboBox, \
QLabel, QSpacerItem, QSizePolicy
except ImportError:
from PySide2.QtCore import Qt, QSortFilterProxyModel, QRegExp
from PySide2.QtGui import QStandardItemModel, QStandardItem
from PySide2.QtWidgets import QWidget, QApplication, QHBoxLayout, QComboBox, \
QLabel, QSpacerItem, QSizePolicy
class SortFilterProxyModel(QSortFilterProxyModel):
def __init__(self, *args, **kwargs):
super(SortFilterProxyModel, self).__init__(*args, **kwargs)
self.setFilterRole(Qt.ToolTipRole) # 根据Qt.ToolTipRole角色过滤
self._model = QStandardItemModel(self)
self.setSourceModel(self._model)
def appendRow(self, item):
self._model.appendRow(item)
def setFilter(self, _):
# 过滤
# self.sender()#发送者
# 获取上一个下拉框中的item_code
item_code = self.sender().currentData(Qt.ToolTipRole)
if not item_code:
return
if item_code.endswith("0000"): # 过滤市
self.setFilterRegExp(QRegExp(item_code[:-4] + "\d\d00"))
elif item_code.endswith("00"): # 过滤市以下
self.setFilterRegExp(QRegExp(item_code[:-2] + "\d\d"))
class CityLinkageWindow(QWidget):
def __init__(self, *args, **kwargs):
super(CityLinkageWindow, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
self.province_box = QComboBox(self, minimumWidth=200) # 市级以上
self.city_box = QComboBox(self, minimumWidth=200) # 市
self.county_box = QComboBox(self, minimumWidth=200) # 市级以下
layout.addWidget(QLabel("省/直辖市/特别行政区", self))
layout.addWidget(self.province_box)
layout.addItem(QSpacerItem(
20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
layout.addWidget(QLabel("市", self))
layout.addWidget(self.city_box)
layout.addItem(QSpacerItem(
20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
layout.addWidget(QLabel("区/县", self))
layout.addWidget(self.county_box)
self.initModel()
self.initSignal()
self.initData()
def initSignal(self):
# 初始化信号槽
self.province_box.currentIndexChanged.connect(
self.city_model.setFilter)
self.city_box.currentIndexChanged.connect(self.county_model.setFilter)
def initModel(self):
# 初始化模型
self.province_model = SortFilterProxyModel(self)
self.city_model = SortFilterProxyModel(self)
self.county_model = SortFilterProxyModel(self)
# 设置模型
self.province_box.setModel(self.province_model)
self.city_box.setModel(self.city_model)
self.county_box.setModel(self.county_model)
def initData(self):
# 初始化数据
datas = open("Data/data.json", "rb").read()
encoding = chardet.detect(datas) or {}
datas = datas.decode(encoding.get("encoding", "utf-8"))
datas = json.loads(datas)
# 开始解析数据
for data in datas:
item_code = data.get("item_code") # 编码
item_name = data.get("item_name") # 名字
item = QStandardItem(item_name)
item.setData(item_code, Qt.ToolTipRole)
if item_code.endswith("0000"): # 4个0结尾的是市级以上的
self.province_model.appendRow(item)
elif item_code.endswith("00"): # 2个0结尾的是市
self.city_model.appendRow(item)
else: # 市以下
self.county_model.appendRow(item)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = CityLinkageWindow()
w.show()
sys.exit(app.exec_())