Checking QRadioButton programmatically?
-
I have two
QRadioButton
for two choices, one of them is selected by default and they are in autoExclusive mode. I also have a setting managing file which saves the choices and loaded them when I reopen my program. Then I turned the choices into enum for better readability. Every time I run the the program, I'll have a switch statement to check and set the radioButton.This works but the problem is in my switch statement, I will have to first set all the other choice to false before setting the last one to true. This will be very annoying when I have a lot of radio buttons in the group. Is there any function that can set all the other choices to false when I set one to true?
switch (savedFormat) { case MyFormat::ChoiceOne: m_ui.mySavingRadioButton_choiceTwo->setChecked(false); m_ui.mySavingRadioButton_choiceOne->setChecked(true); break; case MyFormat::ChoiceTwo: m_ui.mySavingRadioButton_choiceOne->setChecked(false); m_ui.mySavingRadioButton_choiceTwo->setChecked(true); break; }
-
@lansing said in Checking QRadioButton programmatically?:
Is there any function that can set all the other choices to false when I set one to true?
No. But you can search for all radio buttons with QObject::findChildren() and iterate over this.
-
@lansing said in Checking QRadioButton programmatically?:
As @Christian-Ehrlicher has said, you can always visit them in a loop via
QObject::findChildren()
.However, I don't see why this is necessary? You wrote:
they are in autoExclusive mode
I will have to first set all the other choice to false before setting the last one to trueIf your buttons are exclusive, the only thing you need to do is set whichever individual ones to true, and that will set any others in the group to false/unchecked. So I don't understand why you need to do any of the
setChecked(false)
in the code you show?Separately from that. Assuming you have consecutive-numbered
enum
s, you can set up arrays/lists of your radio buttons indexed by theenum
value. Then you can address buttons via theirenum
as an index, instead of having to write code addressingmySavingRadioButton_choiceOne
,mySavingRadioButton_choiceTwo
as individual variables. -
Thanks it works.
QList<QRadioButton *> allRadioButton = m_ui.myRadioButtonLayout->findChildren<QRadioButton *>(); switch (savedFormat) { case MyFormat::ChoiceOne: for (auto radiobutton : allRadioButton) { radiobutton->setChecked(false); } m_ui.mySavingRadioButton_choiceOne->setChecked(true); break; case MyFormat::ChoiceTwo: for (auto radiobutton : allRadioButton) { radiobutton->setChecked(false); } m_ui.mySavingRadioButton_choiceTwo->setChecked(true); break; }
-
btw: QGroupBox also has a function to retrieve all it's buttons.
-
Hi I just tried it again, and indeed it worked without setting any other choices to false. Don't know why it didn't work before...
switch (savedFormat) { case MyFormat::ChoiceOne: m_ui.mySavingRadioButton_choiceOne->setChecked(true); break; case MyFormat::ChoiceTwo: m_ui.mySavingRadioButton_choiceTwo->setChecked(true); break; }