I have four radio buttons, the user must choose one of the four radio buttons.
The problem is each radio button has its own name differs from the other.
How to find out which radio button chosen by the user ?
To get the value of selected radio button, a user-defined function can be created that gets all the radio buttons with the name attribute and finds the radio button selected using the checked property. The checked property returns True if the radio button is selected and False otherwise.
The checked selector is used to select all checked elements in input tag and radio buttons. This selector is used with radio buttons, checkbox and option elements. Example 1: html.
Add the buttons into a GroupBox
and use findChildren
, after this you can use QButtonGroup
or simply iterate through all Buttons list and check name of radiobutton
. It is efficient way because it works with 4 button or 1000, you should write big code if you have many buttons.
void MainWindow::on_pushButton_15_clicked(){
QButtonGroup group;
QList<QRadioButton *> allButtons = ui->groupBox->findChildren<QRadioButton *>();
qDebug() <<allButtons.size();
for(int i = 0; i < allButtons.size(); ++i)
{
group.addButton(allButtons[i],i);
}
qDebug() << group.checkedId();
qDebug() << group.checkedButton();
}
You can use the 'isChecked()' command that all qt buttons support, and check each radio button. Or, you can connect a function to the 'toggled(bool isChecked)' signal, and use that to update a value indicating which of the four radio buttons is checked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With