Qt World Summit: Submit your Presentation

Radio button and if statements C++

  • I read this post on Stackoverflow [1] about remembering the last checked Radio button but I could not implement it in my program.
    I have 5 radio buttons, 1) A , 2) B , 3) C , 4) D , 5) F - I want to have an if statement so when radio button # 1 is selected the below code will be used
    @query.addQueryItem("A", "Good");@

    and if the second radio button is selected this code will be used :

    @query.addQueryItem("B", "Not Bad");@

    so an statement like :

    if (A.isChecked()) {query.addQueryItem("A", "Good");}
    else if (B.isChecked()) {query.addQueryItem("B", "Not Bad");}
    else if (C.isChecked()) {query.addQueryItem("C", "This is Bad");}
    and .....

    I used the toggled option by right clicking on the radio button and followed go to slot, but it did not work
    thank you
    [1] : http://stackoverflow.com/questions/10971973/qt-remembering-the-last-checked-radiobutton

  • What exactly are you expecting to happen with this information? All we know is that something did not work in some unspecified way.

  • For me this sounds like a job for signals and slots, which should be a very easy thing: Either connect all buttons to the same slot and then decide what to do, or connect every button to a different slot (if you were using Qt5 you could use a "lambda expression":http://qt-project.org/wiki/New_Signal_Slot_Syntax ).

    If that's all rubbish, you may explain yourself a little better.

  • There are also examples in the documentation ("e.g. the groupbox example":http://qt-project.org/doc/qt-5.0/qtwidgets/widgets-groupbox.html ).
    The advantage is that the example work and you check them out with the debugger. That might help you to understand the logic better.
    The example you are referring to is not complete. Therefore, it might give you trouble.

    [edit, post updated for broken link, koahnig]

  • Sounds like you are looking for a QButtonGroup. Using that class, you can add all the radio buttons to it, assign each a value, and then get that value from the button group. Instead of dealing with five radio buttons in your query construction code, you only need to deal with one value.

    //in your class header's private section
    enum Quality {
    Excellent, Good, Acceptable, Subpar, Bad

    QButtonGroup* m_bGrpQuality;

    //in your constructor
    m_bGrpQuality = new QButtonGroup(this);
    m_bGrpQuality->addButton(ui->rbExcellent, Excellent);
    m_bGrpQuality->addButton(ui->rbGood, Good);
    //... etc

    //connect to changes in value
    connect(m_bGrpQuality, SIGNAL(buttonClicked(int)), this, SLOT(handleQualityChange(int)));

    // somewhere else in your class
    void handleQualityChange(int quality) {
    Quality q = static_cast<Quality>(quality);
    //do whatever you want with the value.


    Alternatively, you could set the values you need (the "A" and the "Good", etc.) as dynamic properties on the radio buttons themselves. You can do that from designer or from code. Then, you use the buttonClicked(QAbstractButton*) signal and read the dynamic properties of the just clicked button. That way, there is no need for any if statements, enums or switch statements, but it does blur the line between UI and program logic a bit further then you might want.

Log in to reply