Qt - 迭代 QRadioButtons
我正在为学校做一个小组项目。我小组的一名成员创建了一个包含约 75 个单选按钮的窗口。我想在按下按钮时强制所有这些都“清除”或“未选中”。
有谁知道这样做的好方法?我一直在研究 QObjectList 但我不能简单地执行 QObjectList *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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 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).
如果您不喜欢使用 QButtonGroup(太多的设置工作或出于任何其他原因),请使用如下迭代:
很可能您需要操作自动独占(如上面的代码块中所做的那样)取消选中所有单选按钮(另请参阅@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: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)