Qt - iterate through QRadioButtons

I have a group project for the school I'm working on. One member of my group created a window with ~ 75 switches. I want all of them to be “clear” or “unverified” at the click of a button.

Does anyone know a good way to do this? I studied QObjectList, but I can't just do QObjectList * children = new QObjectList (ui-> groupBox-> children ()); and loop them using the for loop, since QObjectList does not have the following method.

I 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 since the iterator is a QObject, setCheckable does not exist, as on a radio button.

Thoughts / tips will be appreciated.

Edit: I will even hint at a way to iterate over variables with similar names. For example, all my radio objects are called RadioButton_1, RadioButton_2, etc.

+3
source share
2 answers

Use QButtonGroup , set it to exclusive (then only one drum will be checked at a time). He also gives you the button that is currently checked, in case you want to also remove it. (so that there are no marked buttons).

Also note that what you probably want to change is the “checked” property, not the “checkable” property (where false means that the button cannot be checked / unchecked at all).

+3
source

If you don’t like using the QButtonGroup (too much customization effort or some other reason), use the following iteration as follows:

 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 previous block of code) to disable all radio buttons (see also @Kristofer answer: fooobar.com/questions/908949 / ... )

+1
source

All Articles