Qt 有没有办法取消选中所有收音机按钮

Qt 有没有办法取消选中所有收音机按钮,qt,Qt,我有一个QGroupBox,里面有几个QradioButton,在某些情况下,我希望所有单选按钮都被取消选中。似乎在做出选择后这是不可能的。你知道我有什么方法可以做到这一点吗?或者我应该添加一个隐藏的单选按钮并检查该单选按钮以获得所需的结果。你可以通过暂时关闭所有单选按钮的自动独占性,取消选中它们,然后重新打开它们来实现这一效果: QRadioButton* rbutton1 = new QRadioButton("Option 1", parent); // ... other code ..

我有一个QGroupBox,里面有几个QradioButton,在某些情况下,我希望所有单选按钮都被取消选中。似乎在做出选择后这是不可能的。你知道我有什么方法可以做到这一点吗?或者我应该添加一个隐藏的单选按钮并检查该单选按钮以获得所需的结果。

你可以通过暂时关闭所有单选按钮的自动独占性,取消选中它们,然后重新打开它们来实现这一效果:

QRadioButton* rbutton1 = new QRadioButton("Option 1", parent);
// ... other code ...
rbutton1->setAutoExclusive(false);
rbutton1->setChecked(false);
rbutton1->setAutoExclusive(true);
您可能想看看如何使用来保持整洁,它可以让您打开和关闭整个按钮组的排他性,而不是自己反复浏览它们:

// 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);
}

但是,正如其他答案所陈述的,你可能想考虑使用复选框,因为单选按钮不是真正用于这类事情的。

< P>如果你使用QGROUPBOX来分组按钮,你就不能使用StutoBuffo(false)函数来取消选中的RealBut纽。您可以在QT文档的一节中了解它。因此,如果您想重置按钮,可以尝试以下操作:

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);
}

您可以禁用ButtonGroup的exclusive属性来重置与ButtonGroup关联的所有按钮,然后启用exclusive属性,这样就不可能进行多个按钮检查。

这违反了单选按钮的原则。根据定义,始终只选择一个。你为什么需要这个?@Thomas:它通常与单选按钮的定义相反,但有时可能有用。考虑一个向导,用户必须做出一个相互排斥的选择。您可以为它们显示单选按钮,但没有默认设置。现在想象一下,他们返回到上一页并更改了一些内容,这意味着您需要重置其余数据,包括再次将单选按钮设置为nothing selected。(理想情况下,在选择其中一个单选按钮之前,向导也无法前进。)这正是我需要它的原因。我需要它,因为应用程序在项目列表中循环,我希望每个项目都加载一个新的UI。如果,您正在对一组按钮进行分组。@richardwb当我执行checked->setChecked(false)时,所选的按钮正在从屏幕上消失。你觉得怎么样?