How to find out which radio button is selected by the user
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)
{
group.addButton(allButtons[i],i);
}
qDebug() << group.checkedId();
qDebug() << group.checkedButton();
}
+6
source to share
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.
0
source to share