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



  • 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


  • Moderators

    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.



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


  • Moderators

    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.



  • [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 :-)



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



  • 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 ;)



  • Do you have a user interface created with Qt Designer?



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

    Yes.



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



  • 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



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



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


Log in to reply
 

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