-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_multiselect_combobox_v2.py
94 lines (69 loc) · 2.85 KB
/
_multiselect_combobox_v2.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
import sys
from Qt import QtWidgets, QtCore, QtGui
class ViewFilter(QtCore.QSortFilterProxyModel):
def filterAcceptsRow(self, source_row, parent_index):
index = parent_index.child(source_row, 0)
checkstate = index.data(QtCore.Qt.CheckStateRole)
return checkstate == QtCore.Qt.Checked
class MultiselectionCombobox(QtWidgets.QFrame):
def __init__(self, parent=None):
super(MultiselectionCombobox, self).__init__(parent)
model = QtGui.QStandardItemModel()
view_filter = ViewFilter()
view_filter.setSourceModel(model)
view = QtWidgets.QListView(self)
view.setModel(view_filter)
view.setResizeMode(QtWidgets.QListView.Adjust)
view.setFlow(QtWidgets.QListView.LeftToRight)
view.setLayoutMode(QtWidgets.QListView.Batched)
arrow_btn = QtWidgets.QPushButton("E", self)
layout = QtWidgets.QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
layout.addWidget(view)
layout.addWidget(arrow_btn)
arrow_btn.clicked.connect(self._on_arrow_click)
self.view = view
self.model = model
self.arrow_btn = arrow_btn
def addItem(self, icon=None, text=None, userData=None):
item = QtGui.QStandardItem()
item.setData(text, QtCore.Qt.DisplayRole)
item.setData(icon, QtCore.Qt.DecorationRole)
item.setData(userData, QtCore.Qt.UserRole)
item.setData(QtCore.Qt.Unchecked, QtCore.Qt.CheckStateRole)
self.model.appendRow(item)
def addItems(self, texts):
new_items = []
for text in texts:
item = QtGui.QStandardItem()
item.setData(text, QtCore.Qt.DisplayRole)
item.setData(QtCore.Qt.Unchecked, QtCore.Qt.CheckStateRole)
new_items.append(item)
self.model.invisibleRootItem().appendRows(new_items)
def _on_arrow_click(self):
menu = QtWidgets.QMenu()
for row in range(self.model.rowCount()):
index = self.model.index(row, 0)
text = index.data(QtCore.Qt.DisplayRole)
action = QtWidgets.QAction(text, menu)
menu.addAction(action)
result = menu.exec_(QtGui.QCursor.pos())
print(result)
class MainWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
texts = ["one", "two"]
combobox = QtWidgets.QComboBox()
# combobox.setMinimumHeight(100)
multiselect_combobox = MultiselectionCombobox(self)
combobox.addItems(texts)
multiselect_combobox.addItems(texts)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(combobox)
layout.addWidget(multiselect_combobox)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())