Qt - 通过QRadioButtons迭代 [英] Qt - iterating through QRadioButtons

查看:325
本文介绍了Qt - 通过QRadioButtons迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个学校的小组项目,我正在工作。我的组的成员创建了一个窗口,其中有〜75个单选按钮。我想强迫所有人在按钮按下时清除或取消选中。

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.

有没有人知道一个很好的方法去做这个?我一直在研究QObjectList,但我不能简单地做QObjectList * children = new QObjectList(ui-> groupBox-> children());并且使用for循环,因为QObjectList似乎没有下一个方法..

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..

我也试图做一些像

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

但是因为iterator是一个QObject,setCheckable不存在像单选按钮。

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

想法/提示会被感激。

编辑:我甚至会提出一个方法以迭代具有相似名称的变量。例如,我所有的radiobuttons都命名为RadioButton_1,RadioButton_2等。

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.

推荐答案

使用 QButtonGroup ,将其设置为exclusive(然后一次只检查一个单选按钮)。它还给你当前选中的按钮,以防你取消选中它。 (根本没有选中的按钮)。

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).

还要注意,你可能想修改的是checked属性,而不是checkable该按钮不能被检查/取消选中)。

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).

这篇关于Qt - 通过QRadioButtons迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆