Qt - 迭代 QRadioButtons

发布于 2024-10-31 03:08:50 字数 678 浏览 6 评论 0原文

我正在为学校做一个小组项目。我小组的一名成员创建了一个包含约 75 个单选按钮的窗口。我想在按下按钮时强制所有这些都“清除”或“未选中”。

有谁知道这样做的好方法?我一直在研究 QObjectList 但我不能简单地执行 Q​​ObjectList *children = new QObjectList(ui->groupBox->children());并使用 for 循环循环它们,因为 QObjectList 似乎没有下一个方法。

我也尝试过做类似的事情

QObjectList *children = new QObjectList(ui->groupBox->children());
for(QObject *iterator = children.first(); iterator!=NULL; children.pop_front()){
    iterator = children.first();
    iterator->at(0)->setCheckabled(false);
}

,但是因为迭代器是 QObject,所以 setCheckable 不存在,就像单选按钮一样。

想法/提示将不胜感激。

编辑:我什至会提示如何迭代具有相似名称的变量。例如,我的所有单选按钮都命名为 RadioButton_1、RadioButton_2 等。

I have a group project for school that I am working on. A member of my group has created a window that has ~75 radio buttons. I want to force all of them to be "clear" or "unchecked" on a button press.

Does anyone know a good way to go about doing this? I have been looking into QObjectList but I can't simply do QObjectList *children = new QObjectList(ui->groupBox->children()); and loop them using a for loop as QObjectList does not appear to have a next method..

I have also tried to do something like

QObjectList *children = new QObjectList(ui->groupBox->children());
for(QObject *iterator = children.first(); iterator!=NULL; children.pop_front()){
    iterator = children.first();
    iterator->at(0)->setCheckabled(false);
}

But because iterator is a QObject, setCheckable does not exist like on a radio button.

Thoughts/hints would be appreciated.

Edit: I'll even take a hint on a way to iterate through variables with similar names. For instance, all of my radiobuttons are named RadioButton_1, RadioButton_2, etc.

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

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

发布评论

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

评论(2

妄想挽回 2024-11-07 03:08:50

使用 QButtonGroup,将其设置为独占(然后在一次)。它还为您提供当前选中的按钮,以防您也想取消选中它。 (根本没有选中的按钮)。

另请注意,您可能想要修改的是“checked”属性,而不是“checkable”(其中 false 表示该按钮根本无法选中/取消选中)。

Use a QButtonGroup, set it to exclusive (then only one radiobutton will be checked at a time). It also gives you the currently checked button, in case you want to uncheck it, too. (to have no checked buttons at all).

Also note that what you probably want to modify is the "checked" property, not "checkable" (where false means that the button can't be checked/unchecked at all).

生来就爱笑 2024-11-07 03:08:50

如果您不喜欢使用 QButtonGroup(太多的设置工作或出于任何其他原因),请使用如下迭代:

QListIterator<QObject *> i(ui->groupBox->children());
while (i.hasNext())
{
    QRadioButton* b = qobject_cast<QRadioButton*>( i.next() );
    if (b > 0 && b->isChecked()) {
        b->setAutoExclusive(false);
        b->setChecked(false);
        b->setAutoExclusive(true);
    }
}

很可能您需要操作自动独占(如上面的代码块中所做的那样)取消选中所有单选按钮(另请参阅@Kristofer 的回答:https://stackoverflow.com/a/9375491/1150303

If you don't like using QButtonGroup (too much setup effort or for whatever other reasons), then use some iteration like this:

QListIterator<QObject *> i(ui->groupBox->children());
while (i.hasNext())
{
    QRadioButton* b = qobject_cast<QRadioButton*>( i.next() );
    if (b > 0 && b->isChecked()) {
        b->setAutoExclusive(false);
        b->setChecked(false);
        b->setAutoExclusive(true);
    }
}

Most likely you need to manipulate autoexclusive (as done in the above code block) to have all radio buttons unchecked (see also @Kristofer's answer: https://stackoverflow.com/a/9375491/1150303)

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