Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to retieve state of item in group box?



  • After RTFM about QGroupBox I still do not know how the retrieve the individual item state "in the box". Say - state of radio button - "isClicked"?
    Yes , I can do that reading individual button state , but that became impractical when the box contains more then two radio buttons.

    The QGroupBox methods are giving no clue how to read the item state, but there are plenty of examples how to build the box using code .

    In pseudocode

    do 
         if ( state of radio button clicked )      real C code ?? 
                 exit 
    while (radio button exist) 
    
    

    Cheers



  • If you have your QRadioButton in your QGroupBox (both designed with QtDesigner), you can just access the radioButton directly.
    ( Something like: ui->radioBtn_test->isChecked())

    @AnneRanch said in How to retieve state of item in group box?:

    but that became impractical when the box contains more then two radio buttons.

    Normally you want to react on toggled() signal and then check what state your button has.

    If you have more than one QRadioButton in one parent widget (-> your QGroupBox layout), they are exclusive by default and represent a virtual buttonGroup

    https://doc.qt.io/qt-5/qabstractbutton.html#autoExclusive-prop

    Why do you want to use a do - while loop?



  • SOLVED
    Create QList using "findChildren" then iterate thru the list using"isChecked"
    Here is my test code:

    QList<QRadioButton *> allRadioButtons =ui->groupBox->findChildren<QRadioButton *>();

    int result = allRadioButtons.at(0)->isChecked();
    result = allRadioButtons.at(1)->isChecked();


  • @AnneRanch

    Not the cleanest solution, but if it works for you... :)



  • @Pl45m4 said in How to retieve state of item in group box?:

    @AnneRanch

    Not the cleanest solution, but if it works for you... :)

    OK , so what is cleaner ?
    I am always open to suggestions.



  • @AnneRanch

    You could react on the button toggle immediately (action happpens right after you change from one button to another).
    OR
    If you just want to check if one specific button is checked, before doing something,
    this:

    ui->yourRadioBtn->isChecked() // if you have your RadioBtn in your Designer ui file
    

    is sufficient.

    For the former way, you could create a QButtonGroup (it's even possible in Designer) and then do something like this:

       ui->rBtn_group->addButton(ui->rBtn_test0, 0); // 0 = id
       ui->rBtn_group->addButton(ui->rBtn_test1, 1); // 1 = id, default starts at -2, decreasing
    
      // only on Qt 5.15+
      connect(ui->rBtn_group, &QButtonsGroup::idToggled, this, &MainWindow::handleRadioBtns);
      
     // if your Qt has a lower Version. than Ver. 5.15, you have to overload / create a custom signal
      void(QButtonGroup::*signal)(int, bool) = &QButtonGroup::buttonToggled;
      connect(ui->rBtn_group, signal, this, &MainWindow::handleRadioBtns);
    
    
    void MainWindow::handleRadioBtns(int id, bool checked)
    {
        if(checked)
        {
            switch(id)
            {
            case -1:
                  // ButtonGroup Error -> -1 = no button or error
                break;
            case 0:
                 // Radio Test0 is checked -> change your settings or do smthg here
                 // ...
                break;
            case 1:
                 // Radio Test1 is checked -> change your settings or do smthg here
                 // ...
                break;
            }
        }
    }
    


  • @Pl45m4 I appreciate your comprehensive reply, but cannot help to ask.
    How does it provide an alternative answer to the original question ?
    It does not.
    It sidesteps the question - it applies the "isChecked" directly to the item, it sidesteps by recommending immediate action when the button is pressed and then implements the switch.

    The post is not wrong, but it does not provide an alternative answer to the original question.

    Ask yourself - if that was a question on an exam - would you get an "A"?

    Cheers



  • @AnneRanch said in How to retieve state of item in group box?:

    Ask yourself - if that was a question on an exam - would you get an "A"?

    Yours even more unlikely, because it's not necessary to find any childs in order to get the state of a radioBtn or checkBox inside a groupBox.

    If you don't like the first solution, well, then don't use it, but my first suggestion as well as my second (which is the easiest way, just to check the state) are better (cleaner) than yours.

    The groupBox is just a layout container and doesn't "hide" its widgets, so that you necessarily need to make use of findChildren.

    Also:
    You have to be careful when using allChildren.at(x) (0 or 1 or whatever index). You don't know the exact order of your items and it may change when you add some new children / radioButtons.



  • @Pl45m4
    If you're interested: this thread has inspired me to raise C++ terseness challenge! in C++ Gurus forum. You may wish to look/comment :)

    [BTW: when using QButtonGroup on exclusive radio buttons as you are here, I assume you're aware QButtonGroup::checkedButton() picks out the single checked radio button without you have to iterate through the buttons testing for checked.]



  • @JonB

    I could swear, checkedButton() wasn't there when I used the signal overload in one of my projects :o

    Edit:
    I've started with 4.8 but according to the documentation it came right with QButtonGroup...


Log in to reply