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 a QRadioButton using an integer parameter
QtWS25 Last Chance

Checking a QRadioButton using an integer parameter

Scheduled Pinned Locked Moved Unsolved General and Desktop
qradiobuttonselect
9 Posts 5 Posters 1.6k 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.
  • C Offline
    C Offline
    Calvin H-C
    wrote on last edited by
    #1

    What is the easiest way to initially check the a QRadioButton using an integer value?

    I have a QDialog created in the design view of Creator with n+1 radio buttons, and I would like to initially check the radio button corresponding to an integer value (from 0 to n).

    I could do this with a switch statement that calls the setChecked() member for the corresponding QRadioButton, but this seems rather clumsy.

    In MFC, one can simply use DDX_Radio() by passing it the resource ID of the first radio button and the integer value. This requires the radio button resource IDs to be sequential, but that is a minor requirement for a simple solution that works for any value of n.

    The radio buttons are in a group box, so I did consider looking into getting a list of children in the hope of being able to select the radio button from a list, but the children() function returned a QObjectList.

    C JonBJ 2 Replies Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      QObject::findChild could work. Name the buttons using the integer identifier.

      Knowing more about the problem might suggest a more appropriate solution. Is this integer known prior to build time?

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      1
      • C Calvin H-C

        What is the easiest way to initially check the a QRadioButton using an integer value?

        I have a QDialog created in the design view of Creator with n+1 radio buttons, and I would like to initially check the radio button corresponding to an integer value (from 0 to n).

        I could do this with a switch statement that calls the setChecked() member for the corresponding QRadioButton, but this seems rather clumsy.

        In MFC, one can simply use DDX_Radio() by passing it the resource ID of the first radio button and the integer value. This requires the radio button resource IDs to be sequential, but that is a minor requirement for a simple solution that works for any value of n.

        The radio buttons are in a group box, so I did consider looking into getting a list of children in the hope of being able to select the radio button from a list, but the children() function returned a QObjectList.

        C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        @Calvin-H-C Further to @jeremy_k's question, how is the GUI constructed? Is it a Designer file or is it built in code?

        If it's built in code then keeping a vector of QRadioButton pointers seems straightforward enough.

        1 Reply Last reply
        0
        • C Calvin H-C

          What is the easiest way to initially check the a QRadioButton using an integer value?

          I have a QDialog created in the design view of Creator with n+1 radio buttons, and I would like to initially check the radio button corresponding to an integer value (from 0 to n).

          I could do this with a switch statement that calls the setChecked() member for the corresponding QRadioButton, but this seems rather clumsy.

          In MFC, one can simply use DDX_Radio() by passing it the resource ID of the first radio button and the integer value. This requires the radio button resource IDs to be sequential, but that is a minor requirement for a simple solution that works for any value of n.

          The radio buttons are in a group box, so I did consider looking into getting a list of children in the hope of being able to select the radio button from a list, but the children() function returned a QObjectList.

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

          @Calvin-H-C
          Do you realise that Qt class QButtonGroup does just this for you?

          In addition, QButtonGroup can map between integers and buttons

          If you are using Designer see https://forum.qt.io/topic/24027/qtdesigner-how-radio-button-group-together for how you can add one at design time.

          C 1 Reply Last reply
          5
          • JonBJ JonB

            @Calvin-H-C
            Do you realise that Qt class QButtonGroup does just this for you?

            In addition, QButtonGroup can map between integers and buttons

            If you are using Designer see https://forum.qt.io/topic/24027/qtdesigner-how-radio-button-group-together for how you can add one at design time.

            C Offline
            C Offline
            Calvin H-C
            wrote on last edited by
            #5

            @JonB said in Checking a QRadioButton using an integer parameter:

            Do you realise that Qt class QButtonGroup does just this for you?

            I assumed this, but could not find an example of how this is done. I'm still struggling with MFC-think. :-(

            The radio buttons are placed in a group box in designer. The constructor of the parent QDialogBox has an integer value available that represents which radio button should be selected when the user open the dialog. It will have the value of 0 to n-1 (in this specific case, 0-5 as there are six radio buttons) . It would be nice if there was a function to be called that I can pass this value to, or at least this value added to the first button's ID (assuming the IDs are consecutive) in order to set the correct one.

            If I am to use QButtonGroup, these are my questions:

            1. Is there a way to create a QButtonGroup object from the QGroupBox object that I can get from the UI?

            2. If I have to just create it in my code, how to I get the buttons (pointer or reference) from the parent QGroupButton?

            JonBJ 1 Reply Last reply
            0
            • C Calvin H-C

              @JonB said in Checking a QRadioButton using an integer parameter:

              Do you realise that Qt class QButtonGroup does just this for you?

              I assumed this, but could not find an example of how this is done. I'm still struggling with MFC-think. :-(

              The radio buttons are placed in a group box in designer. The constructor of the parent QDialogBox has an integer value available that represents which radio button should be selected when the user open the dialog. It will have the value of 0 to n-1 (in this specific case, 0-5 as there are six radio buttons) . It would be nice if there was a function to be called that I can pass this value to, or at least this value added to the first button's ID (assuming the IDs are consecutive) in order to set the correct one.

              If I am to use QButtonGroup, these are my questions:

              1. Is there a way to create a QButtonGroup object from the QGroupBox object that I can get from the UI?

              2. If I have to just create it in my code, how to I get the buttons (pointer or reference) from the parent QGroupButton?

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

              @Calvin-H-C

              1. QGroupBoxes themselves are not visual; they are logical. So I can't comment on "create a QButtonGroup object from the QGroupBox object that I can get from the UI`. I gave you a thread about how to create in Designer, did you read that? It's all I know.

              2. Did you read the linked documentation for QGroupButton? E.g. QAbstractButton *QButtonGroup::button(int id) const

              C 1 Reply Last reply
              0
              • JonBJ JonB

                @Calvin-H-C

                1. QGroupBoxes themselves are not visual; they are logical. So I can't comment on "create a QButtonGroup object from the QGroupBox object that I can get from the UI`. I gave you a thread about how to create in Designer, did you read that? It's all I know.

                2. Did you read the linked documentation for QGroupButton? E.g. QAbstractButton *QButtonGroup::button(int id) const

                C Offline
                C Offline
                Calvin H-C
                wrote on last edited by
                #7

                @JonB

                1. I did read it, but it didn't help. The thread was about another issue where radio buttons in a groupbox without anything else could be selected normally, but if there were other objects in the groupbox, then they couldn't.

                2. I did go through the QButtonGroup documentation before I posted my original question. The function to add requires a pointer to a radio button, and my question was about HOW to get that since I was not creating the radio buttons in code, but had designer do that for me.

                JonBJ Christian EhrlicherC 2 Replies Last reply
                0
                • C Calvin H-C

                  @JonB

                  1. I did read it, but it didn't help. The thread was about another issue where radio buttons in a groupbox without anything else could be selected normally, but if there were other objects in the groupbox, then they couldn't.

                  2. I did go through the QButtonGroup documentation before I posted my original question. The function to add requires a pointer to a radio button, and my question was about HOW to get that since I was not creating the radio buttons in code, but had designer do that for me.

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

                  @Calvin-H-C

                  1. The thread stated how you could create a QButtonGroup from Designer.

                  2. If you do the above I don't know why you would still need "The function to add requires a pointer to a radio button". But if do it in code you would use the variable name you created in Designer for each button to add, like ui->btn... or whatever you named them.

                  1 Reply Last reply
                  0
                  • C Calvin H-C

                    @JonB

                    1. I did read it, but it didn't help. The thread was about another issue where radio buttons in a groupbox without anything else could be selected normally, but if there were other objects in the groupbox, then they couldn't.

                    2. I did go through the QButtonGroup documentation before I posted my original question. The function to add requires a pointer to a radio button, and my question was about HOW to get that since I was not creating the radio buttons in code, but had designer do that for me.

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

                    Name your buttons in designer radioButton_X with X as your id, use a loop in your code to add them to QButtonGroup later on:

                    QButtonGroup *bg = ...
                    for (int i = 1; i <= maxCount; ++i) {
                      auto button = findChild<QRadioButton*>(QString("radioButton_%1").arg(i));
                      if (button) {
                        bg->addButton(button, id);
                      } else {
                        qWarning() << "Something went wrong";
                      }
                    }
                    

                    or directly address them

                    const auto buttons = { ui->radioButton_1, ui->radioButton_2, ... };
                    int id = 1;
                    for (auto button : buttons)
                        bg->addButton(button, id++);
                    

                    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
                    3

                    • Login

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