-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombobox_of_checkboxes.py
52 lines (46 loc) · 1.84 KB
/
combobox_of_checkboxes.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
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 29 20:43:19 2018
@author: PascPeli
"""
from PyQt5 import QtGui, QtCore, QtWidgets
import sys, os
class CheckableComboBox(QtWidgets.QComboBox):
def __init__(self):
super(CheckableComboBox, self).__init__()
self.view().pressed.connect(self.handleItemPressed)
self.setModel(QtGui.QStandardItemModel(self))
def handleItemPressed(self, index):
item = self.model().itemFromIndex(index)
if item.checkState() == QtCore.Qt.Checked:
item.setCheckState(QtCore.Qt.Unchecked)
else:
item.setCheckState(QtCore.Qt.Checked)
class Dialog_01(QtWidgets.QMainWindow):
def __init__(self):
super(QtWidgets.QMainWindow,self).__init__()
myQWidget = QtWidgets.QWidget()
myBoxLayout = QtWidgets.QVBoxLayout()
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
self.ComboBox = CheckableComboBox()
for i in range(3):
self.ComboBox.addItem("Combobox Item " + str(i))
item = self.ComboBox.model().item(i, 0)
item.setCheckState(QtCore.Qt.Unchecked)
self.toolbutton = QtWidgets.QToolButton(self)
self.toolbutton.setText('Select Categories ')
self.toolmenu = QtWidgets.QMenu(self)
for i in range(3):
action = self.toolmenu.addAction("Category " + str(i))
action.setCheckable(True)
self.toolbutton.setMenu(self.toolmenu)
self.toolbutton.setPopupMode(QtWidgets.QToolButton.InstantPopup)
myBoxLayout.addWidget(self.toolbutton)
myBoxLayout.addWidget(self.ComboBox)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())