Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have all radion buttons be unchecked

Tags:

qt

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.

like image 346
yan bellavance Avatar asked Nov 13 '09 20:11

yan bellavance


People also ask

How do you make a radio button unchecked?

To set a radio button to checked/unchecked, select the element and set its checked property to true or false , e.g. myRadio. checked = true . When set to true , the radio button becomes checked and all other radio buttons with the same name attribute become unchecked. Here is the HTML for the examples in this article.

How do I make radio buttons not checked by default?

You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .

Can radio buttons be deselected by default?

Give people control and align with their expectations (Good): It is better to have a selected radio button by default, given that people cannot deselect and set the button back to its original state once one has been selected. A default selection sets the correct user expectation.

How can I set only one radio button checked?

Radio buttons are normally presented in radio groups (a collection of radio buttons describing a set of related options). Only one radio button in a group can be selected at the same time. Note: The radio group must have share the same name (the value of the name attribute) to be treated as a group.


2 Answers

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.

like image 99
richardwb Avatar answered Sep 25 '22 23:09

richardwb


If you're using QGroupBox to group buttons, you can't use the setExclusive(false) function to uncheck the checked RadioButton. You can read about it in QRadioButton section of QT docs. So if you want to reset your buttons, you can try something like this:

QButtonGroup *buttonGroup = new QButtonGroup;
QRadioButton *radioButton1 = new QRadioButton("button1");
QRadioButton *radioButton2 = new QRadioButton("button2");
QRadioButton *radioButton3 = new QRadioButton("button3");

buttonGroup->addButton(radioButton1);
buttonGroup->addButton(radioButton2);
buttonGroup->addButton(radioButton3);

if(buttonGroup->checkedButton() != 0)
{
   // Disable the exclusive property of the Button Group
   buttonGroup->setExclusive(false);

   // Get the checked button and uncheck it
   buttonGroup->checkedButton()->setChecked(false);

   // Enable the exclusive property of the Button Group
   buttonGroup->setExclusive(true);
}

You can disable the exclusive property of the ButtonGroup to reset all the buttons associated with the ButtonGroup, then you can enable the Exclusive property so that multiple button checks won't be possible.

like image 34
Rakesh R Avatar answered Sep 22 '22 23:09

Rakesh R