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 that differs from the others.
How to find out which radio button was 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 that differs from the others.
How to find out which radio button was chosen by the user ?
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();
}
setData()
function to get it later. is it possible to do that. –
Bemuse 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.
The numeric values of the four IDs should be continuous. Given that, call GetCheckedRadioButton to determine which one is selected.
GetCheckedRadioButton
function in Qt5 ? –
Bemuse © 2022 - 2024 — McMap. All rights reserved.
setData()
. – Bemuse