PDA

View Full Version : Slider Value always return 0



Javenne
7th March 2012, 22:13
hi,

I'm trying to make a simple program that gets integers from sliders,
and compute them.

A sample of my code :

data.h

#include <QtGUI>
#include <QObject>

#ifndef DATA_H
#define DATA_H
class Data : public QWidget
{
Q_OBJECT

public:
Data(QWidget *parent = 0);

public slots:
void doSum();

private:
int Z0RealSliderValue;
int ZLRealSliderValue;

QLineEdit *Product;

};

#endif // DATA_H

data.cpp


#include <QtGUI>
#include "data.h"

Data::Data(QWidget *parent)
{

QSlider *Z0RealInput = new QSlider(Qt::Horizontal, this);
Z0RealInput->setRange(0, 150);

QSlider *ZLRealInput = new QSlider(Qt::Horizontal, this);
ZLRealInput->setRange(0, 150);

Z0RealSliderValue = Z0RealInput->value();
ZLRealSliderValue = ZLRealInput->value();

Product = new QLineEdit("Sum");
Product->setReadOnly(true);

QPushButton *Plot = new QPushButton(tr("Plot"),this);
connect(Plot, SIGNAL(clicked()), this, SLOT(doSum()));

//layout etc etc
....

}

void Data::doSum()
{
int Sum = 0;

Sum = ZLRealSliderValue + Z0RealSliderValue;

Product->setText(QString::number(Sum));

}


It kept returning 0.

I have tried to change setting the lineedit text with a constant string, and it worked,
so I assume there is nothing wrong with the connection.

Is it how I retrieve the value which make the code not working?

Thanks in advance.

NullPointer
7th March 2012, 22:31
Hi,

The problem is that you are getting the values before the user can interact with the controls.
You can remove the variables ZLRealSliderValue and Z0RealSliderValue (which you are initializing with the values from just created sliders) and put the QSlider::value() from each in the function doSum instead of the variables...

HTH.

Javenne
8th March 2012, 11:42
Thanks!!

It worked perfectly :)
Didn't realize that my previous code will ignore the interactions to the sliders.

ChrisW67
9th March 2012, 00:38
Your previous code was not ignoring interactions with the sliders. The way it was written there could not possible have been interaction with the sliders between when you created the widget and when you read the value. After that, you never attempted to read the values again.

Javenne
10th March 2012, 01:26
Yes, I have changed my code to

data.h


#include <QtGUI>
#include <QObject>

#ifndef DATA_H
#define DATA_H
class Data : public QWidget
{
Q_OBJECT

public:
Data(QWidget *parent = 0);

public slots:
void doSum();

private:

QSlider *Z0RealInput;
QSlider *ZLRealInput;

int Z0RealSliderValue;
int ZLRealSliderValue;

QLineEdit *Product;

};

#endif // DATA_H


data.cpp

#include <QtGUI>
#include "data.h"

Data::Data(QWidget *parent)
{

Z0RealInput = new QSlider(Qt::Horizontal, this);
Z0RealInput->setRange(0, 150);

ZLRealInput = new QSlider(Qt::Horizontal, this);
ZLRealInput->setRange(0, 150);

Product = new QLineEdit("Sum");
Product->setReadOnly(true);

QPushButton *Plot = new QPushButton(tr("Plot"),this);
connect(Plot, SIGNAL(clicked()), this, SLOT(doSum()));

//layout etc etc
....

}

void Data::doSum()
{
int Sum = 0;

ZLRealSliderValue = ZLRealInput->value();
Z0RealSliderValue = Z0RealInput->value();

Sum = ZLRealSliderValue + Z0RealSliderValue;

Product->setText(QString::number(Sum));

}

And it's working now :)
Thanks