Enable Head/Parent checkbox if any of the children checkboxes are disabled
-
Hello All,
I have multiple checkboxes
In which There is aHeadcheckbox and fewchildrencheckboxes and there are not grouped in any manner i am just referring them as head and childrenSteps what i want
- When i launch the ui it should have all the children checkboxes checked
- if i randomly disable any one of the checkbox it should enable the head checkbox
- if i randomly disable few more checkbox it should keep the head checkbox setChecked(True)
- if i randombly enable any checkbox, it should still keep the head checkbox setChecked(True)
- Once all the children checkboxes are enabled, then only the head checkbox should be disabled
- basically multiple back and forths not on the basis of
clickedandstateChangedbutcheckedandunchecked
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()
if qcheckbox_2.isChecked() and qcheckbox_3.isChecked()it works in a certain order onlyThis approach is very unintuitive, any suggestions would be really helpful
Thank You.
-
Hello All,
I have multiple checkboxes
In which There is aHeadcheckbox and fewchildrencheckboxes and there are not grouped in any manner i am just referring them as head and childrenSteps what i want
- When i launch the ui it should have all the children checkboxes checked
- if i randomly disable any one of the checkbox it should enable the head checkbox
- if i randomly disable few more checkbox it should keep the head checkbox setChecked(True)
- if i randombly enable any checkbox, it should still keep the head checkbox setChecked(True)
- Once all the children checkboxes are enabled, then only the head checkbox should be disabled
- basically multiple back and forths not on the basis of
clickedandstateChangedbutcheckedandunchecked
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()
if qcheckbox_2.isChecked() and qcheckbox_3.isChecked()it works in a certain order onlyThis approach is very unintuitive, any suggestions would be really helpful
Thank You.
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) -
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) -
@SGaist
Thank You I'll give it a try, i tried with for loop but was unsuccessfulYes 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
-
@SGaist
your snippet does the opposite thing, if all are selected ,qcheckbox_1should not be checked, butqcheckbox_1gets checked when i uncheckqcheckbox_2,
if i randomly uncheck any other checkbox,qcheckbox_1does not get enabled@blossomsg
You can see that the code setsqcheckbox_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_2specially.Step through the code in Python debugger or use appropriate
print()statements to follow the logic. -
@JonB
random is easy to understand, me unchecking any check boxes should checkqcheckbox_1, lolbut 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😅

@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() -
B blossomsg has marked this topic as solved on