How to access a member of a QButtonGroup by number?



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





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



  • thank you a lot!

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



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



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



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



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



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

    which type has ui->widget ???



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



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



  • right, it's the type Display...



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



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



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.