tags:

views:

94

answers:

3

I have a QGroupBox with a couple of QRadioButtons inside of it and in certain cases I want all radio buttons to be unchecked. Seems that this is not possible when a selection has been made. Do you know of a way I could do this or should I add a hidden radiobutton and check that onen to get the desired result.

+2  A: 

Would it work to add a radio button with a label like "None"?

Neil Whitaker
Yeah I thought about that
yan bellavance
A: 

In javascript you could iterate the radio buttons and set checked=false. You might consider using checkboxes instead of radio buttons though, since you are going against what radio buttons are used for.

Myles
This isn't JavaScript though, it's Qt specific.
quark
well when the program initially launches none of the radio buttons are selected. CheckBoxes would add too much overhead.
yan bellavance
+4  A: 

You can achieve this effect by temporarily turning off auto exclusivity for all your radio buttons, unchecking them, and then turning them back on:

QRadioButton* rbutton1 = new QRadioButton("Option 1", parent);
// ... other code ...
rbutton1->setAutoExclusive(false);
rbutton1->setChecked(false);
rbutton1->setAutoExclusive(true);

You might want to look at using QButtonGroup to keep things tidier, it'll let you turn exclusivity on and off for an entire group of buttons instead of iterating through them yourself:

// where rbuttons are QRadioButtons with appropriate parent widgets
// (QButtonGroup doesn't draw or layout anything, it's just a container class)
QButtonGroup* group = new QButtonGroup(parent);
group->addButton(rbutton1);
group->addButton(rbutton2);
group->addButton(rbutton3);

// ... other code ...

QAbstractButton* checked = group->checkedButton();
if (checked)
{
    group->setExclusive(false);
    checked->setChecked(false);
    group->setExclusive(true);
}

However, as the other answers have stated, you might want to consider using checkboxes instead, since radio buttons aren't really meant for this sort of thing.

richardwb