Solved 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.view().pressed.connect(self.handleItemPressed) self._changed = False def handleItemPressed(self, index): item = self.model().itemFromIndex(index) if item.checkState() == Qt.Checked: item.setCheckState(Qt.Unchecked) else: item.setCheckState(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: item.setCheckState(Qt.Checked) else: item.setCheckState(Qt.Unchecked)
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.ui.setupUi(self) 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.addItem(self.combo_counterp[index]) 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.
-
@Piyush said in Checkable Combobox: read checked or unchecked items:
self.ui.out_counterp
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