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. Checking QRadioButton programmatically?
QtWS25 Last Chance

Checking QRadioButton programmatically?

Scheduled Pinned Locked Moved Solved General and Desktop
qradiobutton
6 Posts 3 Posters 2.7k Views
  • 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.
  • L Offline
    L Offline
    lansing
    wrote on 20 Jun 2020, 05:02 last edited by lansing
    #1

    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;
        }
    
    
    J 1 Reply Last reply 20 Jun 2020, 07:58
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 20 Jun 2020, 07:22 last edited by
      #2

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

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

      L 1 Reply Last reply 20 Jun 2020, 16:38
      4
      • L lansing
        20 Jun 2020, 05:02

        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;
            }
        
        
        J Offline
        J Offline
        JonB
        wrote on 20 Jun 2020, 07:58 last edited by
        #3

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

        L 1 Reply Last reply 20 Jun 2020, 16:59
        1
        • C Christian Ehrlicher
          20 Jun 2020, 07:22

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

          L Offline
          L Offline
          lansing
          wrote on 20 Jun 2020, 16:38 last edited by
          #4

          @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;
              }
          
          
          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 20 Jun 2020, 16:54 last edited by
            #5

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

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

            1 Reply Last reply
            1
            • J JonB
              20 Jun 2020, 07:58

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

              L Offline
              L Offline
              lansing
              wrote on 20 Jun 2020, 16:59 last edited by
              #6

              @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;
                  }
              
              1 Reply Last reply
              0

              1/6

              20 Jun 2020, 05:02

              • Login

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