PyQT how to get all of the selected checkboxes
-
I have an application with 16 checkboxes on it and based on which ones are checked I will set certain values with that. My problem is that I don't want to have to say
if self.ui.checkbox_1.isChecked(): .... elif self.ui.checkbox2.isChecked(): .... else: ....
for all 16 of the checkboxes. Is there a way to just say something along the lines of
checkbox_list = [] for checkbox in checkbox_container: if checkbox.isChecked(): checkbox_list.append(checkbox)
or is there a built in way to do this?
-
I have an application with 16 checkboxes on it and based on which ones are checked I will set certain values with that. My problem is that I don't want to have to say
if self.ui.checkbox_1.isChecked(): .... elif self.ui.checkbox2.isChecked(): .... else: ....
for all 16 of the checkboxes. Is there a way to just say something along the lines of
checkbox_list = [] for checkbox in checkbox_container: if checkbox.isChecked(): checkbox_list.append(checkbox)
or is there a built in way to do this?
@poordev123
You have to set this up yourself. One way is just to go:self.checkbox_list = [ self.ui.checkbox_1, self.ui.checkbox2, ... ]
once and then re-use that. That's laziest/simplest.
Another way is to use
checkbox_list = whatever_parent_they_all_have.findChildren<QCheckBox *>();
That's C++ syntax, it's something very similar in Python. The function is
QObject.findChildren()
to look up, I think it might be justwidget.findChildren(QCheckBox)
. That does a recursive descent to find all widgets of desired type. -
@poordev123
You have to set this up yourself. One way is just to go:self.checkbox_list = [ self.ui.checkbox_1, self.ui.checkbox2, ... ]
once and then re-use that. That's laziest/simplest.
Another way is to use
checkbox_list = whatever_parent_they_all_have.findChildren<QCheckBox *>();
That's C++ syntax, it's something very similar in Python. The function is
QObject.findChildren()
to look up, I think it might be justwidget.findChildren(QCheckBox)
. That does a recursive descent to find all widgets of desired type.@JonB Thanks for your answer, I appreciate it. One question I have though, by parent do you mean the groupbox that they are in? Or the actual parent object of the checkbox class from qt?
-
I have an application with 16 checkboxes on it and based on which ones are checked I will set certain values with that. My problem is that I don't want to have to say
if self.ui.checkbox_1.isChecked(): .... elif self.ui.checkbox2.isChecked(): .... else: ....
for all 16 of the checkboxes. Is there a way to just say something along the lines of
checkbox_list = [] for checkbox in checkbox_container: if checkbox.isChecked(): checkbox_list.append(checkbox)
or is there a built in way to do this?
@poordev123 said in PyQT how to get all of the selected checkboxes:
I have an application with 16 checkboxes on it and based on which ones are checked I will set certain values with that. My problem is that I don't want to have to say
if self.ui.checkbox_1.isChecked(): .... elif self.ui.checkbox2.isChecked(): .... else: ....
for all 16 of the checkboxes. Is there a way to just say something along the lines of
checkbox_list = [] for checkbox in checkbox_container: if checkbox.isChecked(): checkbox_list.append(checkbox)
These two snippets don't do the same thing. The first one implements an exclusive group. The second allows multiple check boxes to be acted on.
or is there a built in way to do this?
QButtonGroup provides an abstract container into which button widgets can be placed. It does not provide a visual representation of this container (see QGroupBox for a container widget), but instead manages the states of each of the buttons in the group.
-
@JonB Thanks for your answer, I appreciate it. One question I have though, by parent do you mean the groupbox that they are in? Or the actual parent object of the checkbox class from qt?
@poordev123 said in PyQT how to get all of the selected checkboxes:
by parent do you mean the groupbox that they are in?
Yes.
Or you can go down @jeremy_k's
QButtonGroup
approach. Are your checkboxes indeed intended to be exclusive from yourelif
code?