Method one: Using object name to obtain
qradiobutton* pbtn = Qobject_cast (Ui->bg->checkedbutton ());
QString name = Pbtn->objectname ();
if (! Qstring::compare (name, "RadioButton"))
{
Qmessagebox::information (This, "Tips", "Red chosed!", Qmessagebox::ok);
}
else if (! Qstring::compare (name, "Radiobutton_2"))
{
Qmessagebox::information (This, "Tips", "Blue chosed!", Qmessagebox::ok);
}
Else
{
Qmessagebox::information (This, "Tips", "Black chosed!", Qmessagebox::ok);
}
In the code fragment, you first use Qobject_cast to convert the Qabstractionbutton returned by the Checkedbutton () function to its subclass type Qradiobutton. Then, gets the object name of the selected button. This can be obtained by getting objectname this property. A little judgment will tell the result. Note: BG is a manually added Qgroupbutton type, RadioButton and radiobutton_2,radiobutton_3 are all RadioButton controls added to the UI.
Method Two: Get the ID of the button
Code in the constructor (initially select the first button):
Ui->bg->setid (Ui->radiobutton, 0);
Ui->bg->setid (ui->radiobutton_2, 1);
Ui->bg->setid (Ui->radiobutton_3, 2);
Ui->radiobutton->setchecked (TRUE);
The code in the slot function or other function of the response signal:
int a = Ui->bg->checkedid ();
Switch (a)
{
Case 0:
Qmessagebox::information (This, "Tips", "Red chosed!", Qmessagebox::ok);
Break
Case 1:
Qmessagebox::information (This, "Tips", "Blue chosed!", Qmessagebox::ok);
Break
Case 2:
Qmessagebox::information (This, "Tips", "Black chosed!", Qmessagebox::ok);
Break
Default
Break
}