Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out which radio button chosen by the user

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 ?

like image 365
Lion King Avatar asked Oct 11 '14 22:10

Lion King


People also ask

How you can obtain the value of the radio button selected 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.

Which selector is used to select radio which is selected?

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.


2 Answers

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();
}
like image 196
Kosovan Avatar answered Oct 21 '22 16:10

Kosovan


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.

like image 1
SimLeek Avatar answered Oct 21 '22 16:10

SimLeek