Thursday, April 16, 2015

Uncheck all buttons of QButtonGroup

Uncheck all buttons in QButtonGroup


A QButtonGroup can be nice e.g. when you have a set of checkable buttons where only one of them should be checked at any given time. You simply add all the button to the button group and set it to be exclusive and everything else will be handled automagically.
However, sometimes it is necessary to be able to uncheck a button (and therefore all the buttons) of a QButtonGroup, which is unfortunately not directly supported. To work around this limitation you need to disable the exclusive property of the button group before unchecking the button and enable it afterwards again.

You can also use this little class, which hides the boilerplate code behind an uncheckButtons() function:

1 comment:

  1. you can use QButtonBroup removeButton function to remove the QButtonBroup checkButton, and then set checkButton Checked to false, and add this checkButton to QButtonGroup again finally.

    QAbstractButton *checkButton = buttonGroup->checkedButton();
    buttonGroup->removeButton(checkButton);
    checkButton->setChecked(false);
    buttonGroup->addButton(checkButton);

    ReplyDelete