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

Checking QRadioButton programmatically?



  • I have two QRadioButton for two choices, one of them is selected by default and they are in autoExclusive mode. I also have a setting managing file which saves the choices and loaded them when I reopen my program. Then I turned the choices into enum for better readability. Every time I run the the program, I'll have a switch statement to check and set the radioButton.

    This works but the problem is in my switch statement, I will have to first set all the other choice to false before setting the last one to true. This will be very annoying when I have a lot of radio buttons in the group. Is there any function that can set all the other choices to false when I set one to true?

        switch (savedFormat) {
        case MyFormat::ChoiceOne:
            m_ui.mySavingRadioButton_choiceTwo->setChecked(false);
            m_ui.mySavingRadioButton_choiceOne->setChecked(true);
            break;
        case MyFormat::ChoiceTwo:
            m_ui.mySavingRadioButton_choiceOne->setChecked(false);
            m_ui.mySavingRadioButton_choiceTwo->setChecked(true);
            break;
        }
    
    

  • Qt Champions 2019

    @lansing said in Checking QRadioButton programmatically?:

    Is there any function that can set all the other choices to false when I set one to true?

    No. But you can search for all radio buttons with QObject::findChildren() and iterate over this.



  • @lansing said in Checking QRadioButton programmatically?:

    As @Christian-Ehrlicher has said, you can always visit them in a loop via QObject::findChildren().

    However, I don't see why this is necessary? You wrote:

    they are in autoExclusive mode
    I will have to first set all the other choice to false before setting the last one to true

    If your buttons are exclusive, the only thing you need to do is set whichever individual ones to true, and that will set any others in the group to false/unchecked. So I don't understand why you need to do any of the setChecked(false) in the code you show?

    Separately from that. Assuming you have consecutive-numbered enums, you can set up arrays/lists of your radio buttons indexed by the enum value. Then you can address buttons via their enum as an index, instead of having to write code addressing mySavingRadioButton_choiceOne, mySavingRadioButton_choiceTwo as individual variables.



  • @Christian-Ehrlicher

    Thanks it works.

        QList<QRadioButton *> allRadioButton =   
                    m_ui.myRadioButtonLayout->findChildren<QRadioButton *>();    
    
        switch (savedFormat) {
        case MyFormat::ChoiceOne:
            for (auto radiobutton : allRadioButton) {
                    radiobutton->setChecked(false);                
            }
    
            m_ui.mySavingRadioButton_choiceOne->setChecked(true);
            break;
    
        case MyFormat::ChoiceTwo:
            for (auto radiobutton : allRadioButton) {
                    radiobutton->setChecked(false);                
            }
    
            m_ui.mySavingRadioButton_choiceTwo->setChecked(true);
            break;
        }
    
    

  • Qt Champions 2019

    btw: QGroupBox also has a function to retrieve all it's buttons.



  • @JonB

    Hi I just tried it again, and indeed it worked without setting any other choices to false. Don't know why it didn't work before...

        switch (savedFormat) {
        case MyFormat::ChoiceOne:
            m_ui.mySavingRadioButton_choiceOne->setChecked(true);
            break;
        case MyFormat::ChoiceTwo:
            m_ui.mySavingRadioButton_choiceTwo->setChecked(true);
            break;
        }
    

Log in to reply