Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Checkable Combobox: read checked or unchecked items
QtWS25 Last Chance

Checkable Combobox: read checked or unchecked items

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Piyush
    wrote on last edited by VRonin
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • P Piyush

      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.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • P Offline
        P Offline
        Piyush
        wrote on last edited by
        #3

        Thanks @jsulm

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved