Can I un-check a group of RadioBottoms inside a group box?

别说谁变了你拦得住时间么 提交于 2019-12-10 20:19:03

问题


radio bottoms inside a group Box will be treated as a group of bottoms. They are mutual exclusive. How can I clean up their check states??

I have several radio bottoms, one of them are checked. How can I "clean" (uncheck) all radio bottoms?? "setChecked" doesn't work within a group, I tried to do following things but failed.

My code is as following, radioButtom is inside a groupBox, and I want to unchecked it. The first setChecked does works, but the second one doesn't, the radioBottom doesn't been unchecked

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QRadioButton *radioButton;
    ui->setupUi(this);
    radioButton->setChecked(true);
    radioButton->setChecked(false);
}

Where is the problem in my code?


回答1:


The trick is to disable the autoExclusive property before unchecking it, then re-enabling it.

ui->radioButton->setChecked(true);
ui->radioButton->setAutoExclusive(false);
ui->radioButton->setChecked(false);
ui->radioButton->setAutoExclusive(true);

After this, the radioButton is unchecked.




回答2:


In Qt documentation said: A QRadioButton is an option button that can be switched on (checked) or off (unchecked). Radio buttons typically present the user with a "one of many" choice. In a group of radio buttons only one radio button at a time can be checked; if the user selects another button, the previously selected button is switched off. AFAIK I think that you will not be able to check off all QRadioButtons.

In my practice, I have never seen all checked off at once QRadioButtons in one dialog/window. But may be I have mistaken.

As the solution from my side, I may offer you to create one additional QRadioButton, and then hide it, so, when you need to hide all QRadioButton on one widget, you could just setChecked(true) on the hidden one.

Good luck.



来源:https://stackoverflow.com/questions/2816229/can-i-un-check-a-group-of-radiobottoms-inside-a-group-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!