[Solved] QButtonGroup::checkedId () outputs uncommon result
-
I have 3 RadioButtons within a groupbox. Then I create a QButtonGroup which consist those RBs.
when I execute below code I got result as -2 ,-3 , -4 according to the checked button.
@qDebug() << "ID of checked Radio Button" << ui->my_button_group->checkedId();@
How it is possible?
According to the Qt help checkedId() returns checked radiobutton id within group. if not checked any rb then it will be -1.
Thanks in advance.
-
Why do you want a groupbox and a buttonGroup? Doesn't just add container in a container?
The checkedID() should work as in the docs, so are you sure the minus sign isn't an error in your code somewhere? QDebug is nice, but debugging your app with QtCreator is more valuable. -
Hi,
It's the correct behavior. Look at "addButton's documentation":http://qt-project.org/doc/qt-4.8/qbuttongroup.html#addButton it's explained why
-
@Jeroentje@home,
Thank you very much for your reply.
I need to separate those radio buttons from others. Because of that I used the groupbox. And to return the ID of checked radio button I used QButtonGroup.
It is ok, if any method give me the values of those three buttons as 0, 1, 2 when they checked.
I checked my code but didn't find any place which cause to -.
Thanks again.
-
@ SGaist,
Thank you very much for helping me.
bq. If you are also assigning your own ids, use positive values to avoid conflicts.
But I couldn't find way to assign my own ids to radio buttons using Designer. :(
Thanks again..
-
AFAIK you can't (I might be wrong though but I didn't find anything like that)
However you can do it by code.The question is why do you need these id for and precisely why 0 1 and 2 ?
-
SGaist,
I create a 3 Radio buttons to represent a location of a electrical component.
There is 3 possible locations Top, Back and Bottom which can be selected by user. According to the selection I have to update the property.
I have enum like below.
@ enum COMPONENTLOCATION
{
TOP = 0,
BACK = 1,
BOTTOM = 2
};@When user change the preferred location I'm going to set that location as below.
@component->setLocation((ComponentProperties::COMPONENTLOCATION)
ui->location_button_group->checkedId() );@That's why I need 0,1,2..
Thanking you..
-
You have three (maybe more) choices then:
Set the button id programmatically
Change the enum values
Use a hash/map to map the button id's to your enum
-
SGaist,
I used another method. It is working nicely and with less amount of RadioButtons no problem of coding.
I read the text of checked RadioButton.
@ui->location_button_group->checkedButton()->text();@
Then use if else to setLocation.
Thank you very much for your helping hand.
-
You're welcome !
Since the problem is solved, don't forget to update the tread title so other forum users may know a solution has been found :)
-
Ok, I'll do it.. :)