Checking a QRadioButton using an integer parameter
-
What is the easiest way to initially check the a QRadioButton using an integer value?
I have a QDialog created in the design view of Creator with n+1 radio buttons, and I would like to initially check the radio button corresponding to an integer value (from 0 to n).
I could do this with a switch statement that calls the setChecked() member for the corresponding QRadioButton, but this seems rather clumsy.
In MFC, one can simply use DDX_Radio() by passing it the resource ID of the first radio button and the integer value. This requires the radio button resource IDs to be sequential, but that is a minor requirement for a simple solution that works for any value of n.
The radio buttons are in a group box, so I did consider looking into getting a list of children in the hope of being able to select the radio button from a list, but the children() function returned a QObjectList.
-
QObject::findChild could work. Name the buttons using the integer identifier.
Knowing more about the problem might suggest a more appropriate solution. Is this integer known prior to build time?
-
@Calvin-H-C Further to @jeremy_k's question, how is the GUI constructed? Is it a Designer file or is it built in code?
If it's built in code then keeping a vector of QRadioButton pointers seems straightforward enough.
-
@Calvin-H-C
Do you realise that Qt class QButtonGroup does just this for you?In addition, QButtonGroup can map between integers and buttons
If you are using Designer see https://forum.qt.io/topic/24027/qtdesigner-how-radio-button-group-together for how you can add one at design time.
-
@JonB said in Checking a QRadioButton using an integer parameter:
Do you realise that Qt class QButtonGroup does just this for you?
I assumed this, but could not find an example of how this is done. I'm still struggling with MFC-think. :-(
The radio buttons are placed in a group box in designer. The constructor of the parent QDialogBox has an integer value available that represents which radio button should be selected when the user open the dialog. It will have the value of 0 to n-1 (in this specific case, 0-5 as there are six radio buttons) . It would be nice if there was a function to be called that I can pass this value to, or at least this value added to the first button's ID (assuming the IDs are consecutive) in order to set the correct one.
If I am to use QButtonGroup, these are my questions:
-
Is there a way to create a QButtonGroup object from the QGroupBox object that I can get from the UI?
-
If I have to just create it in my code, how to I get the buttons (pointer or reference) from the parent QGroupButton?
-
-
-
QGroupBox
es themselves are not visual; they are logical. So I can't comment on "create a QButtonGroup object from the QGroupBox object that I can get from the UI`. I gave you a thread about how to create in Designer, did you read that? It's all I know. -
Did you read the linked documentation for
QGroupButton
? E.g. QAbstractButton *QButtonGroup::button(int id) const
-
-
-
I did read it, but it didn't help. The thread was about another issue where radio buttons in a groupbox without anything else could be selected normally, but if there were other objects in the groupbox, then they couldn't.
-
I did go through the QButtonGroup documentation before I posted my original question. The function to add requires a pointer to a radio button, and my question was about HOW to get that since I was not creating the radio buttons in code, but had designer do that for me.
-
-
-
The thread stated how you could create a
QButtonGroup
from Designer. -
If you do the above I don't know why you would still need "The function to add requires a pointer to a radio button". But if do it in code you would use the variable name you created in Designer for each button to add, like
ui->btn...
or whatever you named them.
-
-
Christian Ehrlicher Lifetime Qt Championreplied to Calvin H-C on last edited by Christian Ehrlicher
Name your buttons in designer
radioButton_X
with X as your id, use a loop in your code to add them to QButtonGroup later on:QButtonGroup *bg = ... for (int i = 1; i <= maxCount; ++i) { auto button = findChild<QRadioButton*>(QString("radioButton_%1").arg(i)); if (button) { bg->addButton(button, id); } else { qWarning() << "Something went wrong"; } }
or directly address them
const auto buttons = { ui->radioButton_1, ui->radioButton_2, ... }; int id = 1; for (auto button : buttons) bg->addButton(button, id++);