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. Radio button and if statements C++
Forum Updated to NodeBB v4.3 + New Features

Radio button and if statements C++

Scheduled Pinned Locked Moved General and Desktop
5 Posts 5 Posters 7.0k 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.
  • C Offline
    C Offline
    Cody_52
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thEClaw
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          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]

          Vote the answer(s) that helped you to solve your issue(s)

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

            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->setExclusive(true);
            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.

            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