下拉选择联动 #1 @heroxianf

This commit is contained in:
Irony 2018-01-29 00:11:27 +08:00
parent 2b217d5bb8
commit c2b93af1c0
6 changed files with 9853 additions and 1 deletions

View file

@ -10,8 +10,8 @@ encoding//PyQtChart\u7EC3\u4E60/test/ToolTip2.py=utf-8
encoding//QGraphicsView\u7EC3\u4E60/\u4E16\u754C\u5730\u56FE/WorldMap.py=utf-8
encoding//QGraphicsView\u7EC3\u4E60/\u6DFB\u52A0QWidget.py=utf-8
encoding//tmp/\u622A\u56FE\u753B\u77E9\u5F62/DrawRectangle.py=utf-8
encoding//tmp/\u6570\u636E\u8054\u52A8/ComboBox.py=utf-8
encoding//tmp/\u7B80\u5355\u63D2\u4EF6/Widget.py=utf-8
encoding//\u4E0B\u62C9\u9009\u62E9\u8054\u52A8/ComboBox.py=utf-8
encoding//\u5168\u5C40\u70ED\u952E/HotKey.py=utf-8
encoding//\u56FE\u7247\u52A0\u8F7D/LoadImage.py=utf-8
encoding//\u56FE\u7247\u52A0\u8F7D/res_rc.py=utf-8

View file

@ -19,6 +19,7 @@
- [1.14 窗口重启](窗口重启/)
- [1.15 气泡提示](气泡提示/)
- [1.16 自定义import](自定义import/)
- [1.17 下拉选择联动](下拉选择联动/)
### [2.QGraphicsView练习](QGraphicsView练习/)
- [2.1 世界地图](QGraphicsView练习/世界地图)

View file

@ -0,0 +1,113 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2018年1月27日
@author: Irony."[讽刺]
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
@email: 892768447@qq.com
@file: ComboBox
@description:
'''
import json
import sys
from PyQt5.QtCore import Qt, QSortFilterProxyModel, QRegExp
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QWidget, QApplication, QHBoxLayout, QComboBox,\
QLabel, QSpacerItem, QSizePolicy
import chardet
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2018 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
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 Widget(QWidget):
def __init__(self, *args, **kwargs):
super(Widget, 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.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 = Widget()
w.show()
sys.exit(app.exec_())

View file

@ -0,0 +1,12 @@
# QComboBox下拉数据关联
这里简单的做了一个省市区关联的三级联动数据源在data.json中
主要用了QComboBox的setModel设置一个QSortFilterProxyModel过滤模型
并根据唯一编码过滤为了不影响内容显示唯一编码的角色为ToolTipRole
匆忙中写出来,可能有些不完善,或者该方式效率不是很高
# 截图
![截图1](ScreenShot/1.gif)

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

9726
下拉选择联动/data.json Normal file

File diff suppressed because it is too large Load diff