Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved] Using value inside QSliders to calculate and out put result problem

    General and Desktop
    6
    15
    4552
    Loading More Posts
    • 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.
    • P
      Primordium last edited by

      mainwindow.h
      @ QLabel *active;
      QLabel *passive;
      QSlider *passiveslide;
      QSlider *activeslide;
      @

      main.cpp
      @
      calcresult = new QLabel;
      active = new QLabel;
      passive = new QLabel;
      activeslide = new QSlider;
      passiveslide = new QSlider;

      ...

      void MainWindow::on_calca_clicked()
      {
      int activevar = int(active);
      int passivevar = int(passive);
      int sucess = (50 + (activevar5) - (passivevar5));
      ui->calcresult->setNum(sucess);
      }
      @

      both sliders start at 50 so if someone would press the calculator button the result should be 50, and know matter how do change the sliders the result is always "-2630", right now the code is using the text inside the label, i never managed to use int stored in the slider, i know its a newbie question but i come from php and a little python im not used to work with C++.

      Ty all in advance

      1 Reply Last reply Reply Quote 0
      • M
        mlong last edited by

        In lines 11 and 12, casting activevar and passive var to ints does not return the value shown in the label. Instead, it returns the memory addresses stored in the pointers (as integers.) To get the values, you'd want to use

        @int activevar = active->text().toInt();
        int passivevar = passive->text().toInt();@

        You can get the value of a slider with @slider->value(); @

        The docs are your friend. And a bit of catching up on basic C++ concepts will help you go far.

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply Reply Quote 0
        • P
          Primordium last edited by

          Ty for your reply.

          Just a quick question, am I using something wrong in the calculate part, the result is always 50.

          P.S: Started today to learn C++ with tutorials...

          1 Reply Last reply Reply Quote 0
          • M
            mlong last edited by

            I couldn't tell you. The code above at line 13 in your code you posted seems reasonable, but perhaps the values you're getting back for your two variables are 0.

            Good luck with your tutorials.

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply Reply Quote 0
            • G
              giesbert last edited by

              [quote author="Primordium" date="1300161161"]Ty for your reply.
              Just a quick question, am I using something wrong in the calculate part, the result is always 50.
              P.S: Started today to learn C++ with tutorials... [/quote]

              As you use labels, how do you change the values of the labels active and passive?
              As they are labels they contain static text. if both are set initially to 50, your result will always be fifty, sure. It simple math:

              a = 50 + (b5) - (c5)

              b is the value from the label active and c from passive. As you said, both are set to 50... (or some other similar value), if I ask my math skills, it should always return 50 :-)

              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 Reply Quote 0
              • P
                Primordium last edited by

                i get the same result using slide->value() or the label value, the label only shows the current value of the slider, range 1 - 100.

                1 Reply Last reply Reply Quote 0
                • A
                  andre last edited by

                  [quote author="Primordium" date="1300196442"]i get the same result using slide->value() or the label value, the label only shows the current value of the slider, range 1 - 100.[/quote]
                  Better use the slider's value directly then, as that value already has the right type.

                  1 Reply Last reply Reply Quote 0
                  • P
                    Primordium last edited by

                    [quote author="Andre" date="1300197106"]
                    [quote author="Primordium" date="1300196442"]i get the same result using slide->value() or the label value, the label only shows the current value of the slider, range 1 - 100.[/quote]
                    Better use the slider's value directly then, as that value already has the right type.

                    [/quote]

                    @void MainWindow::on_calca_clicked()
                    {
                    int activevar = activeslide->value();
                    int passivevar = passiveslide->value();
                    int sucess = (50 + (activevar5) - (passivevar5));
                    ui->calcresult->setNum(sucess);
                    }@

                    Even if I change the slides values the output is always the same, 50.
                    I was trying to use valueChanged but I'm still getting nowhere. Even using QAbstractSlider.

                    1 Reply Last reply Reply Quote 0
                    • I
                      ivan.todorovich last edited by

                      Something that helps me a lot when this kind of things come up: qDebug().

                      Include the QtDebug header adding the following line to the top of your cpp file.
                      @#include <QtDebug>@

                      And then try something like this:
                      @
                      void MainWindow::on_calca_clicked()
                      {
                      int activevar = activeslide->value();
                      int passivevar = passiveslide->value();
                      qDebug() << activevar << passivevar;
                      int sucess = (50 + (activevar5) - (passivevar5));
                      ui->calcresult->setNum(sucess);
                      }
                      @
                      You should be getting an output indicating the values of both vars.

                      It will help you track your error and learn at the same time ;)

                      o_o Sorry for my rusted english.

                      1 Reply Last reply Reply Quote 0
                      • G
                        goetz last edited by

                        Do you have a user interface created with Qt Designer?

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply Reply Quote 0
                        • P
                          Primordium last edited by

                          [quote author="Volker" date="1300209569"]Do you have a user interface created with Qt Designer?[/quote]

                          Yes.

                          1 Reply Last reply Reply Quote 0
                          • A
                            andre last edited by

                            So... where do you use that code, because in your first posting has some code where you new-ed the widgets yourself...

                            1 Reply Last reply Reply Quote 0
                            • G
                              goetz last edited by

                              Then you are obviously creating new objects, while the user uses the widgets created from the Designer based UI.

                              Time to read "Using a Designer UI File in Your Application":http://doc.qt.nokia.com/4.7/designer-using-a-ui-file.html

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              1 Reply Last reply Reply Quote 0
                              • A
                                andre last edited by

                                That might be it, that's why I requested to see the some more of the relevant code... :)

                                1 Reply Last reply Reply Quote 0
                                • P
                                  Primordium last edited by

                                  Qt forums are very helpfull and friendly.
                                  thank you all for the help.

                                  1 Reply Last reply Reply Quote 0
                                  • First post
                                    Last post