This commit is contained in:
Irony 2018-12-23 13:07:31 +08:00
parent 7930a383a5
commit 6c687af34f
6 changed files with 9862 additions and 9849 deletions

View file

@ -48,10 +48,10 @@ class SortFilterProxyModel(QSortFilterProxyModel):
self.setFilterRegExp(QRegExp(item_code[:-2] + "\d\d")) self.setFilterRegExp(QRegExp(item_code[:-2] + "\d\d"))
class Widget(QWidget): class CityLinkageWindow(QWidget):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(Widget, self).__init__(*args, **kwargs) super(CityLinkageWindow, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self) layout = QHBoxLayout(self)
self.province_box = QComboBox(self, minimumWidth=200) # 市级以上 self.province_box = QComboBox(self, minimumWidth=200) # 市级以上
self.city_box = QComboBox(self, minimumWidth=200) # 市 self.city_box = QComboBox(self, minimumWidth=200) # 市
@ -88,7 +88,7 @@ class Widget(QWidget):
def initData(self): def initData(self):
# 初始化数据 # 初始化数据
datas = open("data.json", "rb").read() datas = open("Data/data.json", "rb").read()
encoding = chardet.detect(datas) or {} encoding = chardet.detect(datas) or {}
datas = datas.decode(encoding.get("encoding", "utf-8")) datas = datas.decode(encoding.get("encoding", "utf-8"))
datas = json.loads(datas) datas = json.loads(datas)
@ -108,6 +108,6 @@ class Widget(QWidget):
if __name__ == "__main__": if __name__ == "__main__":
app = QApplication(sys.argv) app = QApplication(sys.argv)
w = Widget() w = CityLinkageWindow()
w.show() w.show()
sys.exit(app.exec_()) sys.exit(app.exec_())

View file

@ -0,0 +1,12 @@
# QComboBox
## 1、Data Linkage
[View](CityLinkage.py) | [Run](CityLinkage.py)
Three level linkage, data file is data.json
1. use `QComboBox`,`setModel` and `QSortFilterProxyModel` to filter
2. the filter role is `Qt::ToolTipRole`
3. can use `QColumnView` maybe like this example
![CityLinkage](ScreenShot/CityLinkage.gif)

View file

@ -0,0 +1,12 @@
# QComboBox
## 1、下拉数据关联
[查看](CityLinkage.py) | [运行](CityLinkage.py)
一个省市区关联的三级联动数据源在data.json中
1. 主要用了`QComboBox`的`setModel`设置一个`QSortFilterProxyModel`过滤模型
2. 并根据唯一编码过滤,为了不影响内容显示,唯一编码的角色为`ToolTipRole`
3. 用`QColumnView`可以实现类似效果
![CityLinkage](ScreenShot/CityLinkage.gif)

View file

Before

Width:  |  Height:  |  Size: 100 KiB

After

Width:  |  Height:  |  Size: 100 KiB

View file

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