Important: Please read the Qt Code of Conduct -

Checkable Combobox: read checked or unchecked items

  • Hi

    I have created checkable/multi-select combobox using the below class:

    class CheckableComboBox(QComboBox):
        def __init__(self, parent=None):
            super(CheckableComboBox, self).__init__(parent)
            self._changed = False
        def handleItemPressed(self, index):
            item = self.model().itemFromIndex(index)
            if item.checkState() == Qt.Checked:
            self._changed = True
        def hidePopup(self):
            if not self._changed:
                super(CheckableComboBox, self).hidePopup()
            self._changed = False
        def itemChecked(self, index):
            item = self.model().item(index, self.modelColumn())
            return item.checkState() == Qt.Checked
        def setItemChecked(self, index, checked=True):
            item = self.model().item(index, self.modelColumn())
            if checked:

    I created the combobox on the go using the following and added it into the GUI (.ui file) created using Qt Designer:

    class app(QMainWindow):
        def __init__(self):
            super(app, self).__init__()
            self.ui = Ui_MainWindow()
            self.combo_counterp = ['', 'ABC', 'DEF', 'GHI']
            self.file_path = 'ABCD.xlsx' # Enter data file here
            self.countp = 0
            self.out_counterp_val = ['' for x in range(len(self.combo_counterp)-1)]
            self.ui.out_counterp = CheckableComboBox(self)
            for index in range(len(self.combo_counterp)):
                self.ui.out_counterp.setItemChecked(index, False)
            self.ui.formLayout_2.addRow(self.ui.out_counter_lbl, self.ui.out_counterp)

    Now, I want to check the status of number of items (and value of those items) checked/unchecked. However, I cannot call them using the widget name, i.e., self.ui.out_counterp (shows error as app does not have any attribute self.ui.out_counterp). Also, cannot use checkState() {attribute error again}with the ChecableComboBox widget. Seems like I am missing something here. Please help.

  • Lifetime Qt Champion

    @Piyush said in Checkable Combobox: read checked or unchecked items:


    Why dont you store it in another variable like

    self.out_counterp = CheckableComboBox(self)

    There is no need to store it in self.ui

  • Thanks @jsulm

Log in to reply