Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to retieve state of item in group box?
Qt 6.11 is out! See what's new in the release blog

How to retieve state of item in group box?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 2.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #1

    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

    Pl45m4P 1 Reply Last reply
    0
    • A Anonymous_Banned275

      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

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      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?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      4
      • A Offline
        A Offline
        Anonymous_Banned275
        wrote on last edited by
        #3

        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();
        
        Pl45m4P 1 Reply Last reply
        0
        • A Anonymous_Banned275

          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();
          
          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @AnneRanch

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


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          A 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @AnneRanch

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

            A Offline
            A Offline
            Anonymous_Banned275
            wrote on last edited by
            #5

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

            Pl45m4P 1 Reply Last reply
            0
            • A Anonymous_Banned275

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

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #6

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

              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              A 1 Reply Last reply
              1
              • Pl45m4P Pl45m4

                @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;
                        }
                    }
                }
                
                A Offline
                A Offline
                Anonymous_Banned275
                wrote on last edited by
                #7

                @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

                Pl45m4P 1 Reply Last reply
                -2
                • A Anonymous_Banned275

                  @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

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #8

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


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  JonBJ 1 Reply Last reply
                  2
                  • Pl45m4P Pl45m4

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

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

                    Pl45m4P 1 Reply Last reply
                    3
                    • JonBJ JonB

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

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by Pl45m4
                      #10

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


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved