-
Notifications
You must be signed in to change notification settings - Fork 3
/
ComboCheckBox.py
86 lines (74 loc) · 2.84 KB
/
ComboCheckBox.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
# this combo check box class is adopted from https://learnku.com/articles/42618
# author is Bgods
from PyQt5.QtWidgets import QComboBox, QLineEdit, QListWidget, QListWidgetItem, QCheckBox
class ComboCheckBox(QComboBox):
def loadItems(self, items):
self.items = items
self.items.insert(0, 'All')
self.row_num = len(self.items)
self.Selectedrow_num = 0
self.qCheckBox = []
self.qLineEdit = QLineEdit()
self.qLineEdit.setReadOnly(True)
self.qListWidget = QListWidget()
self.addQCheckBox(0)
self.qCheckBox[0].stateChanged.connect(self.All)
for i in range(0, self.row_num):
self.addQCheckBox(i)
self.qCheckBox[i].stateChanged.connect(self.showMessage)
self.setModel(self.qListWidget.model())
self.setView(self.qListWidget)
self.setLineEdit(self.qLineEdit)
def showPopup(self):
select_list = self.Selectlist()
self.loadItems(items=self.items[1:])
for select in select_list:
index = self.items[:].index(select)
self.qCheckBox[index].setChecked(True)
return QComboBox.showPopup(self)
def printResults(self):
list = self.Selectlist()
print(list)
def addQCheckBox(self, i):
self.qCheckBox.append(QCheckBox())
qItem = QListWidgetItem(self.qListWidget)
self.qCheckBox[i].setText(self.items[i])
self.qListWidget.setItemWidget(qItem, self.qCheckBox[i])
def Selectlist(self):
Outputlist = []
for i in range(1, self.row_num):
if self.qCheckBox[i].isChecked() == True:
Outputlist.append(self.qCheckBox[i].text())
self.Selectedrow_num = len(Outputlist)
return Outputlist
def showMessage(self):
Outputlist = self.Selectlist()
self.qLineEdit.setReadOnly(False)
self.qLineEdit.clear()
show = ';'.join(Outputlist)
if self.Selectedrow_num == 0:
self.qCheckBox[0].setCheckState(0)
elif self.Selectedrow_num == self.row_num - 1:
self.qCheckBox[0].setCheckState(2)
else:
self.qCheckBox[0].setCheckState(1)
self.qLineEdit.setText(show)
self.qLineEdit.setReadOnly(True)
def All(self, zhuangtai):
if zhuangtai == 2:
for i in range(1, self.row_num):
self.qCheckBox[i].setChecked(True)
elif zhuangtai == 1:
if self.Selectedrow_num == 0:
self.qCheckBox[0].setCheckState(2)
elif zhuangtai == 0:
self.clear()
def clear(self):
for i in range(self.row_num):
self.qCheckBox[i].setChecked(False)
def currentText(self):
text = QComboBox.currentText(self).split(';')
if text.__len__() == 1:
if not text[0]:
return []
return text