I would like to filter the items in a PyQt5 QCombobox when checking certain checkboxes.
Example:
samplelist=["a1","a2","a3","b1","b2","b3"]
is the list of items of the QCombobox comboBox
and I have two checkboxes checkBoxa
and checkBoxb
. If
checkboxa
checked andcheckboxb
unchecked:comboBox
has the items["a1", "a2", "a3"]
checkboxa
unchecked andcheckboxb
is checked:comboBox
has the items["b1", "b2", "b3"]
checkboxa
unchecked andcheckboxb
is unchecked:comboBox
is emptycheckboxa
checked andcheckboxb
is checked:comboBox
hassamplelist
Does anyone have an idea how I can connect checkboxa
and checkbox
to the comboBox
to achieve the described result?
Minimal example (checkboxes are not connected to the combobox)
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class ExampleWindow(QDialog):
def __init__(self,parent=None):
super(ExampleWindow,self).__init__(parent)
mainLayout=QtWidgets.QGridLayout()
samplelist=["a1","a2","a3","b1","b2","b3"]
# Create combobox and add items.
self.comboBox = QComboBox()
self.comboBox.addItems(samplelist)
#Create checkboxes
self.checkBoxa = QCheckBox("Select a")
self.checkBoxb = QCheckBox("Select b")
mainLayout.addWidget(self.comboBox,1,0,1,2)
mainLayout.addWidget(self.checkBoxa,0,0,1,1)
mainLayout.addWidget(self.checkBoxb,0,1,1,1)
self.setLayout(mainLayout)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = ExampleWindow()
mainWin.show()
sys.exit( app.exec_() )