How to find out which radio button is selected by the user

I have four radio buttons, the user has to select one of four radio buttons.

The problem is that each radio button has its own name, which is different from the other.

How do I know which radio button is selected by the user?


source to share

3 answers

Add buttons to GroupBox

and use findChildren

, after that you can use QButtonGroup

or just loop through all the button lists and check the name radiobutton

. This is an efficient way, since it works with 4 buttons or 1000, you have to write a lot of code if you have a lot of 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)
    qDebug() << group.checkedId();
    qDebug() << group.checkedButton();




The numeric values ​​of the four identifiers must be contiguous. With that in mind, call GetCheckedRadioButton to determine which one is selected.



You can use the isChecked () ' command which all qt buttons support and check each radio button. Or you can wire the function to the "toggled (bool isChecked)" signal and use that to update the value indicating which of the four radio buttons is checked.



All Articles