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. How to create a combobox of checkboxes ?
Qt 6.11 is out! See what's new in the release blog

How to create a combobox of checkboxes ?

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 4 Posters 13.2k Views 1 Watching
  • 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.
  • ? A Former User

    I tried this example found in internet and it works :

    from PyQt4 import QtGui, QtCore
    import sys, os
    
    class CheckableComboBox(QtGui.QComboBox):
        def __init__(self):
            super(CheckableComboBox, self).__init__()
            self.view().pressed.connect(self.handleItemPressed)
            self.setModel(QtGui.QStandardItemModel(self))
    
        def handleItemPressed(self, index):
            item = self.model().itemFromIndex(index)
            if item.checkState() == QtCore.Qt.Checked:
                item.setCheckState(QtCore.Qt.Unchecked)
            else:
                item.setCheckState(QtCore.Qt.Checked)
    
    class Dialog_01(QtGui.QMainWindow):
        def __init__(self):
            super(QtGui.QMainWindow,self).__init__()
            myQWidget = QtGui.QWidget()
            myBoxLayout = QtGui.QVBoxLayout()
            myQWidget.setLayout(myBoxLayout)
            self.setCentralWidget(myQWidget)
            self.ComboBox = CheckableComboBox()
            for i in range(3):
                self.ComboBox.addItem("Combobox Item " + str(i))
                item = self.ComboBox.model().item(i, 0)
                item.setCheckState(QtCore.Qt.Unchecked)
            self.toolbutton = QtGui.QToolButton(self)
            self.toolbutton.setText('Select Categories ')
            self.toolmenu = QtGui.QMenu(self)
            for i in range(3):
                action = self.toolmenu.addAction("Category " + str(i))
                action.setCheckable(True)
            self.toolbutton.setMenu(self.toolmenu)
            self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup)
            myBoxLayout.addWidget(self.toolbutton)
            myBoxLayout.addWidget(self.ComboBox)
    
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        dialog_1 = Dialog_01()
        dialog_1.show()
        dialog_1.resize(480,320)
        sys.exit(app.exec_())
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #7

    @EJWA A list box does not have to be big: it will have a scroll bar if not all entries can be shown at the same time. Adding checkboxes to a combo box could irritate users as it a combo box is there to select one item from n, not m items from n.

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

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #8

      Hello again,

      If I understand there is no solution to do a combobox with checkboxes even like this example :

      from PyQt4 import QtGui, QtCore
      import sys, os
      
      class CheckableComboBox(QtGui.QComboBox):
          def __init__(self):
              super(CheckableComboBox, self).__init__()
              self.view().pressed.connect(self.handleItemPressed)
              self.setModel(QtGui.QStandardItemModel(self))
      
          def handleItemPressed(self, index):
              item = self.model().itemFromIndex(index)
              if item.checkState() == QtCore.Qt.Checked:
                  item.setCheckState(QtCore.Qt.Unchecked)
              else:
                  item.setCheckState(QtCore.Qt.Checked)
      
      class Dialog_01(QtGui.QMainWindow):
          def __init__(self):
              super(QtGui.QMainWindow,self).__init__()
              myQWidget = QtGui.QWidget()
              myBoxLayout = QtGui.QVBoxLayout()
              myQWidget.setLayout(myBoxLayout)
              self.setCentralWidget(myQWidget)
              self.ComboBox = CheckableComboBox()
              for i in range(3):
                  self.ComboBox.addItem("Combobox Item " + str(i))
                  item = self.ComboBox.model().item(i, 0)
                  item.setCheckState(QtCore.Qt.Unchecked)
              self.toolbutton = QtGui.QToolButton(self)
              self.toolbutton.setText('Select Categories ')
              self.toolmenu = QtGui.QMenu(self)
              for i in range(3):
                  action = self.toolmenu.addAction("Category " + str(i))
                  action.setCheckable(True)
              self.toolbutton.setMenu(self.toolmenu)
              self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup)
              myBoxLayout.addWidget(self.toolbutton)
              myBoxLayout.addWidget(self.ComboBox)
      
      if __name__ == '__main__':
          app = QtGui.QApplication(sys.argv)
          dialog_1 = Dialog_01()
          dialog_1.show()
          dialog_1.resize(480,320)
          sys.exit(app.exec_())
      
      mrjjM 1 Reply Last reply
      0
      • ? A Former User

        Hello again,

        If I understand there is no solution to do a combobox with checkboxes even like this example :

        from PyQt4 import QtGui, QtCore
        import sys, os
        
        class CheckableComboBox(QtGui.QComboBox):
            def __init__(self):
                super(CheckableComboBox, self).__init__()
                self.view().pressed.connect(self.handleItemPressed)
                self.setModel(QtGui.QStandardItemModel(self))
        
            def handleItemPressed(self, index):
                item = self.model().itemFromIndex(index)
                if item.checkState() == QtCore.Qt.Checked:
                    item.setCheckState(QtCore.Qt.Unchecked)
                else:
                    item.setCheckState(QtCore.Qt.Checked)
        
        class Dialog_01(QtGui.QMainWindow):
            def __init__(self):
                super(QtGui.QMainWindow,self).__init__()
                myQWidget = QtGui.QWidget()
                myBoxLayout = QtGui.QVBoxLayout()
                myQWidget.setLayout(myBoxLayout)
                self.setCentralWidget(myQWidget)
                self.ComboBox = CheckableComboBox()
                for i in range(3):
                    self.ComboBox.addItem("Combobox Item " + str(i))
                    item = self.ComboBox.model().item(i, 0)
                    item.setCheckState(QtCore.Qt.Unchecked)
                self.toolbutton = QtGui.QToolButton(self)
                self.toolbutton.setText('Select Categories ')
                self.toolmenu = QtGui.QMenu(self)
                for i in range(3):
                    action = self.toolmenu.addAction("Category " + str(i))
                    action.setCheckable(True)
                self.toolbutton.setMenu(self.toolmenu)
                self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup)
                myBoxLayout.addWidget(self.toolbutton)
                myBoxLayout.addWidget(self.ComboBox)
        
        if __name__ == '__main__':
            app = QtGui.QApplication(sys.argv)
            dialog_1 = Dialog_01()
            dialog_1.show()
            dialog_1.resize(480,320)
            sys.exit(app.exec_())
        
        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #9

        @EJWA
        The main issue is that it will close when you Check one item.
        If you hax that, you then have the issue of how to close it.

        So overall as @jsulm says, its not a very good idea as
        it breaks the standard for the operating system.

        Unless you mean, you just want user to check one and then its fine it closes.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #10

          So, it means that there is no solution other than using a list widget which can be open when you select multiple elements ?

          mrjjM 1 Reply Last reply
          0
          • ? A Former User

            So, it means that there is no solution other than using a list widget which can be open when you select multiple elements ?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #11

            @EJWA
            well you can hax it if you really feel like it

            from PyQt4 import QtCore, QtGui    
            app = QtGui.QApplication([])
            
            class Combo(QtGui.QComboBox):
                def __init__(self, *args, **kwargs):
                    super(Combo, self).__init__()
                    self.addItems(['Item_1','Item_2','Item_3','Item_4','Item_5'])
                    self.show()
            
                def hidePopup (self):
                    pass
            
            inst=Combo()
            sys.exit(app.exec_())
            

            https://stackoverflow.com/questions/25733891/how-to-cause-qcombobox-pull-down-menu-to-stay-open

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #12

              Thanks very much. What about the tool button ? It is a good idea ?

               self.toolbutton = QtGui.QToolButton(self)
                      self.toolbutton.setText('Select Categories ')
                      self.toolmenu = QtGui.QMenu(self)
                      for i in range(3):
                          action = self.toolmenu.addAction("Category " + str(i))
                          action.setCheckable(True)
                      self.toolbutton.setMenu(self.toolmenu)
                      self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup)
              
              mrjjM 1 Reply Last reply
              0
              • ? A Former User

                Thanks very much. What about the tool button ? It is a good idea ?

                 self.toolbutton = QtGui.QToolButton(self)
                        self.toolbutton.setText('Select Categories ')
                        self.toolmenu = QtGui.QMenu(self)
                        for i in range(3):
                            action = self.toolmenu.addAction("Category " + str(i))
                            action.setCheckable(True)
                        self.toolbutton.setMenu(self.toolmenu)
                        self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup)
                
                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #13

                @EJWA

                You mean to use toolbutton to popup menu ?
                or toolbutton inside combo box ?

                Using a popup menu might suffer from same issue to close when one is selected/checked.

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #14

                  the same functions as using a combobox with checkboxes

                  mrjjM 1 Reply Last reply
                  0
                  • ? A Former User

                    the same functions as using a combobox with checkboxes

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #15

                    @EJWA

                    Both ways can work even if not usual function.

                    Also if its rare user need to select multiple Categories its maybe ok.

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #16

                      Can I keep the toolbutton menu open when selecting multiple categories ?

                      mrjjM 1 Reply Last reply
                      0
                      • ? A Former User

                        Can I keep the toolbutton menu open when selecting multiple categories ?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #17

                        @EJWA

                        well yes but its more haxy than combo box
                        https://stackoverflow.com/questions/2050462/prevent-a-qmenu-from-closing-when-one-of-its-qaction-is-triggered/11571550#11571550
                        you might need to use event filter

                        Why dont u just use a dialog ?
                        Click button.
                        Dialog opens with ListView,
                        use checks one or more Categories.
                        User press ok to close.

                        1 Reply Last reply
                        2
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #18

                          Hello, it seems that this code doesn't work because I design the interface with QT creator, so I must generate the py file from the ui using the pyuic4 and then modify the py file. I am trying now this example and I

                          class CheckableComboBox(QtGui.QComboBox):
                              def __init__(self):
                                  super(CheckableComboBox, self).__init__()
                                  self.view().pressed.connect(self.handleItemPressed)
                                  self.setModel(QtGui.QStandardItemModel(self))
                          
                              def handleItemPressed(self, index):
                                  item = self.model().itemFromIndex(index)
                                  if item.checkState() == QtCore.Qt.Checked:
                                      item.setCheckState(QtCore.Qt.Unchecked)
                                  else:
                                      item.setCheckState(QtCore.Qt.Checked)
                          self.imploc_extension = CheckableComboBox()
                                
                                  for i in range(4):
                                      self.imploc_extension.addItem("csv")
                                      self.imploc_extension.addItem("xls")
                                      self.imploc_extension.addItem("mtx")
                                      self.imploc_extension.addItem("txt")
                                      item = self.imploc_extension.model().item(i, 0)
                                      item.setCheckState(QtCore.Qt.Unchecked)
                          
                          jsulmJ 1 Reply Last reply
                          0
                          • ? A Former User

                            Hello, it seems that this code doesn't work because I design the interface with QT creator, so I must generate the py file from the ui using the pyuic4 and then modify the py file. I am trying now this example and I

                            class CheckableComboBox(QtGui.QComboBox):
                                def __init__(self):
                                    super(CheckableComboBox, self).__init__()
                                    self.view().pressed.connect(self.handleItemPressed)
                                    self.setModel(QtGui.QStandardItemModel(self))
                            
                                def handleItemPressed(self, index):
                                    item = self.model().itemFromIndex(index)
                                    if item.checkState() == QtCore.Qt.Checked:
                                        item.setCheckState(QtCore.Qt.Unchecked)
                                    else:
                                        item.setCheckState(QtCore.Qt.Checked)
                            self.imploc_extension = CheckableComboBox()
                                  
                                    for i in range(4):
                                        self.imploc_extension.addItem("csv")
                                        self.imploc_extension.addItem("xls")
                                        self.imploc_extension.addItem("mtx")
                                        self.imploc_extension.addItem("txt")
                                        item = self.imploc_extension.model().item(i, 0)
                                        item.setCheckState(QtCore.Qt.Unchecked)
                            
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #19

                            @EJWA Modifying generated code isn't a good idea as your modifications will be lost next time the code is generated...

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

                            1 Reply Last reply
                            0
                            • ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #20

                              Finally,

                              I will use listwidget, it us easier. Thank you for all your help.

                              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