我可以取消选中组框中的一组 RadioBottom 吗?

发布于 2024-09-01 09:21:28 字数 509 浏览 5 评论 0原文

组 Box 内的单选底部将被视为一组底部。它们是互斥的。我怎样才能清理他们的检查状态?

我有几件收音机底部,其中一件经过检查。 我怎样才能“清洁”(取消选中)所有收音机底部? “setChecked”在组内不起作用,我尝试做以下事情但失败了。

我的代码如下,radioButtom位于groupBox内,我想取消选中它。 第一个 setChecked 确实有效,但第二个不行,radioBottom 没有被取消选中

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

我的代码中的问题在哪里?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2024-09-08 09:21:28

诀窍是在取消选中 autoExclusive 属性之前禁用它,然后重新启用它。

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

此后,radioButton 将被取消选中。

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.

箜明 2024-09-08 09:21:28

在 Qt 文档中说:QRadioButton 是一个选项按钮,可以打开(选中)或关闭(未选中)。单选按钮通常向用户提供“众多之一”的选择。在一组单选按钮中,一次只能选中一个单选按钮;如果用户选择另一个按钮,则先前选择的按钮将被关闭。 AFAIK我认为您将无法选中所有 QRadioButtons。

在我的实践中,我从未见过在一个对话框/窗口中一次性勾选所有 QRadioButtons。但也许我错了。

作为我这边的解决方案,我可能会建议您创建一个额外的 QRadioButton,然后隐藏它,因此,当您需要隐藏一个小部件上的所有 QRadioButton 时,您只需 setChecked(true)隐藏的那个。

祝你好运。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文