Qt - iterating through QRadioButtons
Asked Answered
C

2

3

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.

Caryophyllaceous answered 8/4, 2011 at 17:11 Comment(1)
Here is a similar, more general question about iterating through objects of a particular type in a layout.Trujillo
L
3

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

Linin answered 8/4, 2011 at 17:22 Comment(3)
Oh wow, I feel foolish for not having found that in the documentation. Is there an easy way to convert from QGroupBox to QButtonGroup? He made the whole thing using the UI editor otherwise we could simply change types..Caryophyllaceous
A QButtonGroup is not widget, just a QObject managing button states. So the buttons can stay in the group box. Having exclusive buttons is not enough? That should be default if you add them to any widget, including group boxes.Linin
Ah. Awesome, I was looking for a widget to add to the UI. Very much appreciate the help! Got my problem solved :)Caryophyllaceous
A
1

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://mcmap.net/q/1061510/-qradiobutton-check-uncheck-issue-in-qt)

Annulate answered 11/7, 2015 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.