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. [Solved] Using value inside QSliders to calculate and out put result problem

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

Scheduled Pinned Locked Moved General and Desktop
15 Posts 6 Posters 5.0k Views
  • 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 Offline
    P Offline
    Primordium
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      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
      0
      • P Offline
        P Offline
        Primordium
        wrote on last edited by
        #3

        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
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          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
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            [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
            0
            • P Offline
              P Offline
              Primordium
              wrote on last edited by
              #6

              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
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                [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
                0
                • P Offline
                  P Offline
                  Primordium
                  wrote on last edited by
                  #8

                  [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
                  0
                  • I Offline
                    I Offline
                    ivan.todorovich
                    wrote on last edited by
                    #9

                    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
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      Do you have a user interface created with Qt Designer?

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

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        Primordium
                        wrote on last edited by
                        #11

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

                        Yes.

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

                          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
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #13

                            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
                            0
                            • A Offline
                              A Offline
                              andre
                              wrote on last edited by
                              #14

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

                              1 Reply Last reply
                              0
                              • P Offline
                                P Offline
                                Primordium
                                wrote on last edited by
                                #15

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

                                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