Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Enable Head/Parent checkbox if any of the children checkboxes are disabled
Qt 6.11 is out! See what's new in the release blog

Enable Head/Parent checkbox if any of the children checkboxes are disabled

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
6 Posts 3 Posters 929 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.
  • blossomsgB Offline
    blossomsgB Offline
    blossomsg
    wrote on last edited by
    #1

    Hello All,

    I have multiple checkboxes
    In which There is a Head checkbox and few children checkboxes and there are not grouped in any manner i am just referring them as head and children

    Steps what i want

    1. When i launch the ui it should have all the children checkboxes checked
    2. if i randomly disable any one of the checkbox it should enable the head checkbox
    3. if i randomly disable few more checkbox it should keep the head checkbox setChecked(True)
    4. if i randombly enable any checkbox, it should still keep the head checkbox setChecked(True)
    5. Once all the children checkboxes are enabled, then only the head checkbox should be disabled
    6. basically multiple back and forths not on the basis of clicked and stateChanged but checked and unchecked
    from PySide2 import QtWidgets
    from PySide2 import QtGui
    from PySide2 import QtCore
    
    
    
    def checks():  
    #    all_checks=[qcheckbox_2, qcheckbox_3, qcheckbox_4, qcheckbox_5]
    #    for x in all_checks:
        if qcheckbox_2.isChecked() and qcheckbox_3.isChecked():
            qcheckbox_1.setChecked(False)
        else:
            qcheckbox_1.setChecked(True)
    #            if qcheckbox_1.isChecked():
    #                pass
    #            else:           
    #                qcheckbox_1.setChecked(True)
    
    
    
    win_wid = QtWidgets.QWidget()
    qcheckbox_1 = QtWidgets.QCheckBox("Hello this a push button")
    qcheckbox_2 = QtWidgets.QCheckBox("Hello this is helper button_2")
    qcheckbox_3 = QtWidgets.QCheckBox("Hello this is helper button_3")
    qcheckbox_4 = QtWidgets.QCheckBox("Hello this is helper button_4")
    qcheckbox_5 = QtWidgets.QCheckBox("Hello this is helper button_5")
    qcheckbox_2.setChecked(True)
    qcheckbox_3.setChecked(True)
    qcheckbox_4.setChecked(True)
    qcheckbox_5.setChecked(True)
    
    qcheckbox_2.clicked.connect(checks)
    #qcheckbox_3.clicked.connect(check_3)
    #qcheckbox_.setFixedSize(200, 50)
    layout = QtWidgets.QVBoxLayout()
    layout.addWidget(qcheckbox_1)
    layout.addWidget(qcheckbox_2)
    layout.addWidget(qcheckbox_3)
    layout.addWidget(qcheckbox_4)
    layout.addWidget(qcheckbox_5)
    win_wid.setLayout(layout)
    win_wid.show()
    
    

    checkboxes.gif

    if qcheckbox_2.isChecked() and qcheckbox_3.isChecked() it works in a certain order only

    This approach is very unintuitive, any suggestions would be really helpful

    Thank You.

    SGaistS 1 Reply Last reply
    0
    • blossomsgB blossomsg

      Hello All,

      I have multiple checkboxes
      In which There is a Head checkbox and few children checkboxes and there are not grouped in any manner i am just referring them as head and children

      Steps what i want

      1. When i launch the ui it should have all the children checkboxes checked
      2. if i randomly disable any one of the checkbox it should enable the head checkbox
      3. if i randomly disable few more checkbox it should keep the head checkbox setChecked(True)
      4. if i randombly enable any checkbox, it should still keep the head checkbox setChecked(True)
      5. Once all the children checkboxes are enabled, then only the head checkbox should be disabled
      6. basically multiple back and forths not on the basis of clicked and stateChanged but checked and unchecked
      from PySide2 import QtWidgets
      from PySide2 import QtGui
      from PySide2 import QtCore
      
      
      
      def checks():  
      #    all_checks=[qcheckbox_2, qcheckbox_3, qcheckbox_4, qcheckbox_5]
      #    for x in all_checks:
          if qcheckbox_2.isChecked() and qcheckbox_3.isChecked():
              qcheckbox_1.setChecked(False)
          else:
              qcheckbox_1.setChecked(True)
      #            if qcheckbox_1.isChecked():
      #                pass
      #            else:           
      #                qcheckbox_1.setChecked(True)
      
      
      
      win_wid = QtWidgets.QWidget()
      qcheckbox_1 = QtWidgets.QCheckBox("Hello this a push button")
      qcheckbox_2 = QtWidgets.QCheckBox("Hello this is helper button_2")
      qcheckbox_3 = QtWidgets.QCheckBox("Hello this is helper button_3")
      qcheckbox_4 = QtWidgets.QCheckBox("Hello this is helper button_4")
      qcheckbox_5 = QtWidgets.QCheckBox("Hello this is helper button_5")
      qcheckbox_2.setChecked(True)
      qcheckbox_3.setChecked(True)
      qcheckbox_4.setChecked(True)
      qcheckbox_5.setChecked(True)
      
      qcheckbox_2.clicked.connect(checks)
      #qcheckbox_3.clicked.connect(check_3)
      #qcheckbox_.setFixedSize(200, 50)
      layout = QtWidgets.QVBoxLayout()
      layout.addWidget(qcheckbox_1)
      layout.addWidget(qcheckbox_2)
      layout.addWidget(qcheckbox_3)
      layout.addWidget(qcheckbox_4)
      layout.addWidget(qcheckbox_5)
      win_wid.setLayout(layout)
      win_wid.show()
      
      

      checkboxes.gif

      if qcheckbox_2.isChecked() and qcheckbox_3.isChecked() it works in a certain order only

      This approach is very unintuitive, any suggestions would be really helpful

      Thank You.

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      So basically, qcheckbox_1 should not be checked only when all other check boxes are checked, correct ?

      Not the most efficient but might be clearer:

      check_boxes = [qcheckbox_2, qcheckbox_3, qcheckbox_4]
      values = [check_box.isChecked() for check_box in check_boxes]
      all_checked = all(values)
      
      qcheckbox_1.setChecked(not all_checked)   
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      blossomsgB 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        So basically, qcheckbox_1 should not be checked only when all other check boxes are checked, correct ?

        Not the most efficient but might be clearer:

        check_boxes = [qcheckbox_2, qcheckbox_3, qcheckbox_4]
        values = [check_box.isChecked() for check_box in check_boxes]
        all_checked = all(values)
        
        qcheckbox_1.setChecked(not all_checked)   
        
        blossomsgB Offline
        blossomsgB Offline
        blossomsg
        wrote on last edited by
        #3

        @SGaist
        Thank You I'll give it a try, i tried with for loop but was unsuccessful

        Yes if all of them are checked, qcheckbox_1 should be unchecked, but if qcheckbox_2, qcheckbox_3, etc are unchecked randomly, qcheckbox_1 should get checked

        blossomsgB 1 Reply Last reply
        0
        • blossomsgB blossomsg

          @SGaist
          Thank You I'll give it a try, i tried with for loop but was unsuccessful

          Yes if all of them are checked, qcheckbox_1 should be unchecked, but if qcheckbox_2, qcheckbox_3, etc are unchecked randomly, qcheckbox_1 should get checked

          blossomsgB Offline
          blossomsgB Offline
          blossomsg
          wrote on last edited by
          #4

          @SGaist
          your snippet does the opposite thing, if all are selected ,qcheckbox_1 should not be checked, but qcheckbox_1 gets checked when i uncheck qcheckbox_2,
          if i randomly uncheck any other checkbox, qcheckbox_1 does not get enabled

          JonBJ 1 Reply Last reply
          0
          • blossomsgB blossomsg

            @SGaist
            your snippet does the opposite thing, if all are selected ,qcheckbox_1 should not be checked, but qcheckbox_1 gets checked when i uncheck qcheckbox_2,
            if i randomly uncheck any other checkbox, qcheckbox_1 does not get enabled

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @blossomsg
            You can see that the code sets qcheckbox_1's checked state to off if all other checkboxes are checked and to on if any other checkbox is not checked. Which does not seem to correspond to your "opposite" comment.

            but qcheckbox_1 gets checked when i uncheck qcheckbox_2

            Yes that is what you asked for.

            if i randomly uncheck any other checkbox, qcheckbox_1 does not get enabled

            I don't know what "randomly" means here, but you can see the code does not treat qcheckbox_2 specially.

            Step through the code in Python debugger or use appropriate print() statements to follow the logic.

            1 Reply Last reply
            0
            • blossomsgB Offline
              blossomsgB Offline
              blossomsg
              wrote on last edited by
              #6

              @JonB
              random is easy to understand, me unchecking any check boxes should check qcheckbox_1, lol

              but honestly my bad, i did not connect the other checkboxes to the function, silly of me. I thought connecting one checkbox is looping for all checkbox, dumb move😅checkboxes_2.gif

              @SGaist sorry i doubted your code, it works amazingly😁

              from PySide2 import QtWidgets
              from PySide2 import QtGui
              from PySide2 import QtCore
              
              
              
              def checks():  
                  check_boxes = [qcheckbox_2, qcheckbox_3, qcheckbox_4, qcheckbox_5]
                  values = [check_box.isChecked() for check_box in check_boxes]
                  print values
                  all_checked = all(values)
                  print all_checked
              
                  qcheckbox_1.setChecked(not all_checked)
              
              win_wid = QtWidgets.QWidget()
              qcheckbox_1 = QtWidgets.QCheckBox("Hello this a head button")
              qcheckbox_2 = QtWidgets.QCheckBox("Hello this is helper button_2")
              qcheckbox_3 = QtWidgets.QCheckBox("Hello this is helper button_3")
              qcheckbox_4 = QtWidgets.QCheckBox("Hello this is helper button_4")
              qcheckbox_5 = QtWidgets.QCheckBox("Hello this is helper button_5")
              qcheckbox_2.setChecked(True)
              qcheckbox_3.setChecked(True)
              qcheckbox_4.setChecked(True)
              qcheckbox_5.setChecked(True)
              
              qcheckbox_2.clicked.connect(checks)
              qcheckbox_3.clicked.connect(checks)
              qcheckbox_4.clicked.connect(checks)
              qcheckbox_5.clicked.connect(checks)
              
              layout = QtWidgets.QVBoxLayout()
              layout.addWidget(qcheckbox_1)
              layout.addWidget(qcheckbox_2)
              layout.addWidget(qcheckbox_3)
              layout.addWidget(qcheckbox_4)
              layout.addWidget(qcheckbox_5)
              win_wid.setLayout(layout)
              win_wid.show()
              
              
              
              1 Reply Last reply
              0
              • blossomsgB blossomsg has marked this topic as solved on

              • Login

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