Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use QRadioButton



  • Hello,
    I want to have 4 QRadioButton.
    When the 1st is checked, the 2nd is unchecked.
    When the 3rd is checked, the 4th is unchecked.

    Right now, when the 1st is checked, all the rest are unchecked.

    I guess the question is, who should be the parent of these RadioButtons?



  • @stretchthebits
    QRadioButton is defaultly auto-exclusive.

    If auto-exclusivity is enabled, checkable buttons that belong to the same parent widget behave as if they were part of the same exclusive button group. In an exclusive button group, only one button can be checked at any time; checking another button automatically unchecks the previously checked one.

    So you need to let the 1st and 2nd belong to the same parent widget and the 3rd and 4th to another.
    Or you can use QButtonGroup to manage the exclusivity.



  • Alright, I made a QButtonGroup and added each radio button to it by doing myQButtonGroup->addButton and called setExclusion(false) and now, I can turn the radi button on or off the way I want it.

    Thanks.



  • @stretchthebits

    I want to have 4 QRadioButton.
    When the 1st is checked, the 2nd is unchecked.
    When the 3rd is checked, the 4th is unchecked.

    and called setExclusion(false) and now, I can turn the radi button on or off the way I want it.

    So with setExclusive(false) the user can now check all 4 four of them, or uncheck all 4 of them, or any other combination?

    For the record, from what you say you desire, you should have two, separate groups, with exclusivity within each of the groups. You may then either put each pair of buttons in its own QButtonGroup, or put each pair inside their own dummy "holding" parent QWidget.



  • @stretchthebits
    If you only want to totally turn off the auto-exclusivity, you can simply call setAutoExclusive(false) to every button.
    I think there's no need to useQButtonGroup in that case.


Log in to reply