Important: Please read the Qt Code of Conduct -

How to get all instances of QRadioButton in QGroupBox?

  • 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?

  • @SPlatten

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

    Or use QButtonGroup Class.

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

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion


    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.

  • @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 {
        } eGroupParams;

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

Log in to reply