Is there a way to have all radion buttons be unchecked

后端 未结 2 620
你的背包
你的背包 2021-02-07 04:20

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 h

2条回答
  •  情书的邮戳
    2021-02-07 05:07

    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.

提交回复
热议问题