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 get all instances of QRadioButton in QGroupBox?
Forum Updated to NodeBB v4.3 + New Features

How to get all instances of QRadioButton in QGroupBox?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I have a two radio buttons which I've added to a QGroupBox, I am implementing an undo feature for this I need to get all instances of a QRadioButton in the group.

    I've created a QGridLayout and added this to the QGroupBox, the radio buttons are added to the QGridLayout, this all work and when running the radio buttons function as a group but I want to iterate through all the radio buttons in the group.

    How do I do this?

    Kind Regards,
    Sy

    JonBJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I have a two radio buttons which I've added to a QGroupBox, I am implementing an undo feature for this I need to get all instances of a QRadioButton in the group.

      I've created a QGridLayout and added this to the QGroupBox, the radio buttons are added to the QGridLayout, this all work and when running the radio buttons function as a group but I want to iterate through all the radio buttons in the group.

      How do I do this?

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

      @SPlatten

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

      Or use QButtonGroup Class.

      SPlattenS 1 Reply Last reply
      3
      • JonBJ JonB

        @SPlatten

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

        Or use QButtonGroup Class.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        @JonB said in How to get all instances of QRadioButton in QGroupBox?:

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

        Thank you, however that returns a list with 0 items.

        Kind Regards,
        Sy

        Christian EhrlicherC 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @JonB said in How to get all instances of QRadioButton in QGroupBox?:

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

          Thank you, however that returns a list with 0 items.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @SPlatten said in How to get all instances of QRadioButton in QGroupBox?:

          however that returns a list with 0 items.

          Then this groupBox does not contain any QRadioButtons.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          SPlattenS 1 Reply Last reply
          4
          • Christian EhrlicherC Christian Ehrlicher

            @SPlatten said in How to get all instances of QRadioButton in QGroupBox?:

            however that returns a list with 0 items.

            Then this groupBox does not contain any QRadioButtons.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #5

            @Christian-Ehrlicher , then how come the radio buttons function as a group? Clicking on one works and the other is unchecked, click back on the original does the same.

            Both radio buttons are in the same layout and the layout is added to the group.

            Kind Regards,
            Sy

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Show some code where it does not work. A simple example, not your actual code.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              SPlattenS 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                Show some code where it does not work. A simple example, not your actual code.

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by SPlatten
                #7

                @Christian-Ehrlicher , I have managed to get something working which is a variation of JonB's code:

                QObject* pobjParent = pobjRdo->parent();
                if ( pobjParent != nullptr ) {
                    QList<QRadioButton *> lstRadioButtons = pobjParent->findChildren<QRadioButton *>();
                
                    foreach( QRadioButton* pobjRdoChild, lstRadioButtons ) {
                

                The list returned here does contain the radio buttons.

                Kind Regards,
                Sy

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  If you need to grab the parent then there's something wrong going on. Please show us your group box code.

                  That said, you might also want to consider doing a widget subclass which provides this information through a getter rather than starting to use findChildren.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  SPlattenS 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    Hi,

                    If you need to grab the parent then there's something wrong going on. Please show us your group box code.

                    That said, you might also want to consider doing a widget subclass which provides this information through a getter rather than starting to use findChildren.

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #9

                    @SGaist, I've created a simple demo application using Qt Creator and the UI interface just placing a group box on a form then a Horizontal layout into the group box.

                    I've then added 4 radio buttons, two inside the Horizontal layout and two inside the group box but outside the horizontal layout.

                    The result is that the two inside the horizontal layout are paired and work correctly, the two outside the horizontal layout are also paired but not related in any way to the two inside the horizontal layout, so this all works correctly which means my code is wrong.

                    I will take another look and see what I'm doing wrong, I thought I was doing the same in that the sequence is from this XML:

                    	<entry col="0" row="3" control="radiobutton" label="Sex:" id="enSXmale"
                    		   text="Male" group="sex,0,0,left,true" dbfield="vcSex"/>
                    	<entry col="0" row="3" control="radiobutton" id="enSXfemale" text="Female"
                    		   group="sex,1,0,left,false" dbfield="vcSex"/>
                    	<entry col="0" row="3" control="radiobutton" id="enSXother" text="Other"
                    		   group="sex3,1,0,left,false" dbfield="vcSex"/>
                    

                    In the above, the group attribute contains the parameters for creating a group and placing a control in it. Parameters:

                        typedef enum {
                            GROUP_NAME_IDX,
                            GROUP_COLUMN_IDX,
                            GROUP_ROW_IDX,
                            GROUP_PARAM_COUNT,
                            GROUP_HALIGN_IDX = GROUP_PARAM_COUNT,
                            GROUP_DEFAULT_IDX
                        } eGroupParams;
                    

                    Kind Regards,
                    Sy

                    SPlattenS 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @SGaist, I've created a simple demo application using Qt Creator and the UI interface just placing a group box on a form then a Horizontal layout into the group box.

                      I've then added 4 radio buttons, two inside the Horizontal layout and two inside the group box but outside the horizontal layout.

                      The result is that the two inside the horizontal layout are paired and work correctly, the two outside the horizontal layout are also paired but not related in any way to the two inside the horizontal layout, so this all works correctly which means my code is wrong.

                      I will take another look and see what I'm doing wrong, I thought I was doing the same in that the sequence is from this XML:

                      	<entry col="0" row="3" control="radiobutton" label="Sex:" id="enSXmale"
                      		   text="Male" group="sex,0,0,left,true" dbfield="vcSex"/>
                      	<entry col="0" row="3" control="radiobutton" id="enSXfemale" text="Female"
                      		   group="sex,1,0,left,false" dbfield="vcSex"/>
                      	<entry col="0" row="3" control="radiobutton" id="enSXother" text="Other"
                      		   group="sex3,1,0,left,false" dbfield="vcSex"/>
                      

                      In the above, the group attribute contains the parameters for creating a group and placing a control in it. Parameters:

                          typedef enum {
                              GROUP_NAME_IDX,
                              GROUP_COLUMN_IDX,
                              GROUP_ROW_IDX,
                              GROUP_PARAM_COUNT,
                              GROUP_HALIGN_IDX = GROUP_PARAM_COUNT,
                              GROUP_DEFAULT_IDX
                          } eGroupParams;
                      
                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by SPlatten
                      #10

                      ok, I think I've messed up in the order I'm creating the widgets...will report back when I confirm it.

                      Kind Regards,
                      Sy

                      1 Reply Last reply
                      0

                      • Login

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