PyQt5 – Combo Box with check-able items
In this article we will see how we can make a combo box who have check-able items. By default when we create a combo box its items are not check-able i.e we can select any one item at a time although we can create such combo box by editing the combo box class. Below is the representation of how check-able combo box look like.
In order to do this we have to make a new editable combo box class which inherits the combo box and adds a new feature of check able combo box, below is the syntax of new class
class CheckableComboBox(QComboBox): def __init__(self, parent = None): super(CheckableComboBox, self).__init__(parent) self.view().pressed.connect(self.handleItemPressed) self.setModel(QStandardItemModel(self)) def handleItemPressed(self, index): item = self.model().itemFromIndex(index) if item.checkState() == Qt.Checked: item.setCheckState(Qt.Unchecked) else: item.setCheckState(Qt.Checked)
Below is the implementation –