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 access a member of a QButtonGroup by number?

How to access a member of a QButtonGroup by number?

Scheduled Pinned Locked Moved General and Desktop
17 Posts 4 Posters 7.6k Views 1 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.
  • A Offline
    A Offline
    Anna
    wrote on last edited by
    #1

    Hello to all,

    I am a beginner in Qt and try to implement a little application.

    First I've read all the docs and topics concernig buttons and QButtonGroup, but I couldn't resolve the problem on my own...
    So I'm asking you: I would like to check which one of these three radioButtons is checked and write the number of the button in a array (position [0])

    this is the group I've created:

    @QButtonGroup* groupGr = new QButtonGroup(this);
    groupGr->addButton(ui->radioButtonGr1);
    groupGr->addButton(ui->radioButtonGr2);
    groupGr->addButton(ui->radioButtonGr3);@

    and this is how I tried to write the number of the button into the array:

    @char* Anfangsphase[2];
    int i;
    for(i=1; i < 4; i++)
    {
    if (groupGr->button(i-1)->isChecked() == true)
    Anfangsphase[0] = "i";
    }@

    Does anybody know how it works?

    Thank you a lot for helping!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stukdev
      wrote on last edited by
      #2

      Use "QButtonGroup::checkedId ()":http://doc.qt.nokia.com/stable/qbuttongroup.html#checkedId

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Better use addButton(QAbstractButton*, int) though. That allows you to assign your own ID's, so you actually know which button has which ID.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Anna
          wrote on last edited by
          #4

          thank you a lot!

          both ways worked actually - so I'm happy :-)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Anna
            wrote on last edited by
            #5

            now, I've got another issue with the button groups. but it is also a member variable problem:

            the buttons are elements of my main class gui, so is another gui widget. I would like to read the checked button's id by a function in another class. so I need the ID as a variable in the child ui's class. but of course I can't access it...

            this is where I get the ID in the main class:
            @void Andon::inputStart()
            {

            QButtonGroup* groupT = new QButtonGroup(this); //Tag 1 bis 3
            groupT->addButton(ui->radioButtonT1, 1);
            groupT->addButton(ui->radioButtonT2, 2);
            groupT->addButton(ui->radioButtonT3, 3);
            
            Tag = groupT->checkedId();
            

            }@

            then i've tried this:

            @void Display::setSpielTag()
            {
            SpielTag = Andon->Tag;
            }@

            can somebody help me?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Please, pick up a C++ book first. What you are trying to do is far, very far from how C++ works.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Anna
                wrote on last edited by
                #7

                Andre, I would love to! and probably I'll do that very soon. but right now I need to finish one project as the deadline is tomorrow and I've started with c++ and qt one week ago. you see, everything is new to me but of course I would like to learn it right.

                I know this question was not what people like to read, as this is not object oriented and I've read your answers to similiar questions. so it would be better to implement a get function, right?
                how can I implement that?

                or would a signal be better? I've tried this, but there must be mistakes as it doesn't work:
                @connect(groupT,SIGNAL(buttonClicked(int)), ui->widget, SLOT(setSpielTag(int)));@

                with set SpielTag(int) as a public slot in the Display class (:
                @void Display::setSpielTag(int Tag)
                {
                t = Tag;
                }@

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Anna
                  wrote on last edited by
                  #8

                  @mods: could you "unsolve" this thread, please?
                  or should I start a new topic?

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #9

                    typically, a new question should have a new thread, but in your case:

                    which type has ui->widget ???

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Anna
                      wrote on last edited by
                      #10

                      Gerolf,
                      it is a gui. you already helped me with creating this gui in this thread:
                      http://developer.qt.nokia.com/forums/viewthread/9706

                      what's the correct approach to get the ID?

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on last edited by
                        #11

                        I meant a class name. does the class that ui->widget points to has the slot setSpielTag? is it of type Display?

                        Nokia Certified Qt Specialist.
                        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          Anna
                          wrote on last edited by
                          #12

                          right, it's the type Display...

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            giesbert
                            wrote on last edited by
                            #13

                            do you get some error or warnign output in the debugger or on the console?

                            Nokia Certified Qt Specialist.
                            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              Anna
                              wrote on last edited by
                              #14

                              the direct access from the class display doesn't work (I can't even select the andon class variable, of course)
                              and the slot thing doesn't work either. my app stops as there is a silly value for t, so it wasn't set by the signal)

                              but I'd really rather write a get function or something like this in order to access this member variable, as this seems to be a useful thing to learn ;)
                              how would that work?

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                giesbert
                                wrote on last edited by
                                #15

                                This is really basic C++. You should use a book for that.
                                If you need a getter, make the ButtoGroup pointer a member of the class and access it in the getter.

                                Nokia Certified Qt Specialist.
                                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  Anna
                                  wrote on last edited by
                                  #16

                                  gerolf, the advice to use a book I already got it a few posts earlier and I agree with it. this is something I already know even without the first time telling me.
                                  what I don't know is how to solve my problem. so what would be really useful to me is probably an example how I could implement the getter.

                                  maybe someone can give me one?

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    andre
                                    wrote on last edited by
                                    #17

                                    Off-topic:
                                    The point of helping for many people here (ok, at least for me, let me not pretend to speak for others here) is that it is a learning experience for everyone involved. Feeding you complete answers, also on non-Qt issues, does not qualify as such. That is why Gerolf and I suggest you learn C++ before trying your hand at Qt.

                                    I am sorry you have a deadline in a language and toolkit you know very little about, but that is your problem, not ours. Personally, I would have invested in a paid consultant for a situation like that. That would get you straight answers and timely responses, and ready-to-use pieces of code.

                                    On-topic then:
                                    Make Tag a property on the class: a private variable to keep the value (you could perhaps use your button group itself for that), a getter method to retreive the value, and a setter method to set it. The getter method will return the value of your private variable, while the setter one will manipulate its value (and its representation, if that is separate from each other). Optionally, you can add a changed signal for the value, to let other objects know that the value has been changed.

                                    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