PDA

View Full Version : Getting the value from a spinbox



X-man
26th September 2007, 04:41
Seems easy, but doesn't work for some reason...

I have a Qspinbox and I am trying to get its value and assign it to a variable. The code seems simple, as in:

int value1;
value1=valueSpinBox1->value();
qDebug("Value1 = %.4f", value2);

The qDebug was added in order to check if value1 gets the value of the spinbox. I get no errors or warnings, but I also don't get any message in the debug window, so I assume that the value is not passed on.

Right below that, I have a connection between the valueChanged signal of the spinbox and the display slot of an LCD, and it works just fine.

Basically, I need to take the values of five spinboxes and eg. add them and get a result.

Any suggestions? Thanks in advance.

jpn
26th September 2007, 07:31
So, you're on Windows? Try enabling console by adding:

QT += console
to the .pro file and rebuilding the project. Oh, and shouldn't "value2" be "value1"? ;)

X-man
26th September 2007, 07:41
Pardon the typo (can't expect a lot at 3am) - it is value1....

I am on MSVC 2005. Are you suggesting to edit the .pro file and adding a line with "QT +=console"? Is that suppose to fix the fact that I don't get a qDebug? Because inside the same program I am using qDebug with no problem...

jpn
26th September 2007, 08:10
Pardon the typo (can't expect a lot at 3am) - it is value1....
You should consider sleeping then.. ;)

I am on MSVC 2005. Are you suggesting to edit the .pro file and adding a line with "QT +=console"? Is that suppose to fix the fact that I don't get a qDebug? Because inside the same program I am using qDebug with no problem...
Alright, then you can forget about adding "QT += console". Are you sure that the mentioned line gets actually executed? What if you include <QtDebug> and make it

qDebug() << valueSpinBox1->value();
?

X-man
26th September 2007, 08:42
"qDebug() <<valueSpinBoxJ1->value();" doesn't change anything... I am sure there's something wrong with my coding, but unable to find it...

The weird thing is that the same spinbox sends a signal (valueChanged) to both the lcd(display) inside the same .cpp but also to another slot inside another .cpp - and still cannot get the bloody value assigned to a variable - maybe it's my lack of good OOP/C++ programming, but still... How would you do it anyway? (you are the MSc in S/W anyway :P)

X-man

PS1. Thanks for your time, give a hug to Rocky from me (my dog is a huge mini pincher fan!)
PS2. Should I sent you my files to take a quick look?

jpn
26th September 2007, 09:58
Perhaps you forgot Q_OBJECT (http://doc.trolltech.com/4.3/qobject.html#Q_OBJECT) macro from the class that declares a custom slot? Can you seen any warnings in the debug output by QObject::connect()? For example something like this:


Object::connect: No such slot ClassName::slotName()

You can paste/attach relevant parts of the code directly on the forums if you'd like us to take a look at it.

PS. I'm still just a student. And Rocky says hi. :)

wysota
26th September 2007, 10:17
You might want to try to show a QMessageBox instead of using qDebug(), just in case something is wrong with qDebug. Looks like the slot is not called at all, so it would be nice if you shew us the line of code where you make the respective signal-slot connection.

X-man
26th September 2007, 18:22
Ok, I am putting up a short version of my window.h and window.cpp with just two spinboxes and no connection to other .cpp files.

window.h

#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>
class QGroupBox;
class QLabel;
class QSpinBox;
class QLCDNumber;

class Window : public QWidget
{
Q_OBJECT

public:
Window();

public slots:
void setMinimum1(int value);
void setMaximum1(int value);
void setMinimum2(int value);
void setMaximum2(int value);

private:
void createControls(const QString &title);

QGroupBox *controlsGroup;
QLabel *J1;
QLabel *J2;
QLabel *minimumLabel;
QLabel *maximumLabel;
QLabel *valueLabel;

QSpinBox *minimumSpinBoxJ1;
QSpinBox *maximumSpinBoxJ1;
QSpinBox *valueSpinBoxJ1;
QSpinBox *minimumSpinBoxJ2;
QSpinBox *maximumSpinBoxJ2;
QSpinBox *valueSpinBoxJ2;

QLCDNumber *lcd1;
QLCDNumber *lcd2;

};
#endif

and window.cpp


#include <QtGui>
#include "window.h"

Window::Window()
{
createControls(tr("Controls"));

connect(minimumSpinBoxJ1, SIGNAL(valueChanged(int)), this, SLOT(setMinimum1(int)));
connect(maximumSpinBoxJ1, SIGNAL(valueChanged(int)), this, SLOT(setMaximum1(int)));
connect(minimumSpinBoxJ2, SIGNAL(valueChanged(int)), this, SLOT(setMinimum2(int)));
connect(maximumSpinBoxJ2, SIGNAL(valueChanged(int)), this, SLOT(setMaximum2(int)));

QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(controlsGroup,1,0);
mainLayout->addWidget(sizesGroup,2,0);
mainLayout->addWidget(coinWidget,0,0,1,0);
setLayout(mainLayout);
minimumSpinBoxJ1->setValue(0);
maximumSpinBoxJ1->setValue(0);
valueSpinBoxJ1->setValue(0);
minimumSpinBoxJ2->setValue(0);
maximumSpinBoxJ2->setValue(0);
valueSpinBoxJ2->setValue(0);

}

void Window::createControls(const QString &title)
{
controlsGroup = new QGroupBox(title);
controlsGroup->setAlignment(Qt::AlignCenter);

J1 = new QLabel(tr("J1"));
J1->setAlignment(Qt::AlignCenter);
J2 = new QLabel(tr("J2"));
J2->setAlignment(Qt::AlignCenter);

minimumLabel = new QLabel(tr("Angle - minimum value:"));
maximumLabel = new QLabel(tr("Angle - maximum value:"));
valueLabel = new QLabel (tr("Angle - current value:"));
minimumSpinBoxJ1 = new QSpinBox;
minimumSpinBoxJ1->setRange(-500,500);
minimumSpinBoxJ1->setSingleStep(1);
maximumSpinBoxJ1 = new QSpinBox;
maximumSpinBoxJ1->setRange(-500,500);
maximumSpinBoxJ1->setSingleStep(1);
valueSpinBoxJ1 = new QSpinBox;
valueSpinBoxJ1->setSingleStep(1);
minimumSpinBoxJ2 = new QSpinBox;
minimumSpinBoxJ2->setRange(-500,500);
minimumSpinBoxJ2->setSingleStep(1);
maximumSpinBoxJ2 = new QSpinBox;
maximumSpinBoxJ2->setRange(-500,500);
maximumSpinBoxJ2->setSingleStep(1);
valueSpinBoxJ2 = new QSpinBox;
valueSpinBoxJ2->setSingleStep(1);

QLCDNumber *lcd1 = new QLCDNumber(3);
lcd1->setSegmentStyle(QLCDNumber::Flat);
QLCDNumber *lcd2 = new QLCDNumber(4);
lcd2->setSegmentStyle(QLCDNumber::Flat);
QLCDNumber *lcd3 = new QLCDNumber(4);

connect(valueSpinBoxJ1,SIGNAL(valueChanged(int)),l cd1,SLOT(display(int)));
connect(valueSpinBoxJ2,SIGNAL(valueChanged(int)),l cd2,SLOT(display(int)));

QGridLayout *controlsLayout = new QGridLayout;
controlsLayout->addWidget(J1,1,1);
controlsLayout->addWidget(J2,1,2);

controlsLayout->addWidget(lcd1,5,1);
controlsLayout->addWidget(lcd2,5,2);

controlsLayout->addWidget(minimumLabel, 2, 0);
controlsLayout->addWidget(maximumLabel, 3, 0);
controlsLayout->addWidget(valueLabel, 4, 0);

controlsLayout->addWidget(minimumSpinBoxJ1, 2, 1);
controlsLayout->addWidget(maximumSpinBoxJ1, 3, 1);
controlsLayout->addWidget(valueSpinBoxJ1, 4, 1);
controlsLayout->addWidget(minimumSpinBoxJ2, 2, 2);
controlsLayout->addWidget(maximumSpinBoxJ2, 3, 2);
controlsLayout->addWidget(valueSpinBoxJ2, 4, 2);
controlsGroup->setLayout(controlsLayout);
}


void Window::setMinimum1(int value)
{
valueSpinBoxJ1->setMinimum(value);
}

void Window::setMaximum1(int value)
{
valueSpinBoxJ1->setMaximum(value);
}

void Window::setMinimum2(int value)
{
valueSpinBoxJ2->setMinimum(value);
}

void Window::setMaximum2(int value)
{
valueSpinBoxJ2->setMaximum(value);
}

What I have to do is this:

a) fetch the value of SpinBoxJ1 and assign it to a variable (say varJ1)
b) fetch the value of SpinBoxJ2 and assign it to another variable (say varJ2)
c) Make a calculation (say valJ1+varJ2) and assign the result to another variable (say var J12)
d) Send that result to a new QLCD or elsewhere

Obviously, varJ1 and varJ2 (and varJ12) should change every time I change any of the two spinboxes.

Sorry for the long post (I tried to include only the essential parts of the code), waiting for your feedback.

X-man

jpn
26th September 2007, 20:11
Add a new slot, for example Window::calculate(). Connect both spin boxes to that slot:


connect(valueSpinBoxJ1,SIGNAL(valueChanged(int)),t his,SLOT(calculate()));
connect(valueSpinBoxJ2,SIGNAL(valueChanged(int)),l cd2,SLOT(calculate()));

In the slot, retrieve values and add them together:


void Window::calculate()
{
int valJ1 = valueSpinBoxJ1->value();
int valJ2 = valueSpinBoxJ2->value();
lcd3->display(valJ1 + valJ2);
}

X-man
27th September 2007, 02:59
Thanks JPN. So, I added "void calculate();" under the public slots inside window.h.

I then added the two connections (with the correction of "lcd2" to "this", I assumed), under my "Window::Window()" inside the window.cpp and added at the end the "void Window::calculate() {.....}"

First time run, it crashed - something not working with QLCDNumber.h it said. I changed that and instead of sending the value to an LCD, I used
qDebug("Val12 = %.4f", valJ1+valJ2) to check what's going on...

In the debug window, I do get "Val12 = 0". The good part is that "Val12 = 0" reappears everytime I change something in the two spinboxes. The sad part is that it just doesn't have the correct value - stays zero. I suppose that "calculate" is done every time I change something in one of the two spinboxes, the valueChanged connection works OK, but valJ1 and valJ2 don't get the value of their spinbox from the valueSpinBoxJ1/2->value thing... which is not good at all... how can that be?

WHAT'S WRONG!?!?!?!:confused:

X-man

jpn
27th September 2007, 08:42
Oh yes, "lcd2" was a typo, sorry. There's at least a classical mix up with local and member variables. You have declared a member variable:

QLCDNumber *lcdX;
But you are assigning to a local variable:

QLCDNumber *lcdX = new QLCDNumber(X);
Now if you try to dereference it in another function:

ldcX->something();
You'll get a crash because you are using an uninitialized pointer. So you should make it:

lcdX = new QLCDNumber(X);

X-man
27th September 2007, 14:52
Right on, JPN, about my mistake on the local/member variables - that is now corrected.

So, I am now using:


void Window::calculate()
{
int valJ1 = valueSpinBoxJ1->value();
int valJ2 = valueSpinBoxJ2->value();
int X = valJ1+valJ2;
lcd3->display(X);
qDebug("X = %.4f",X);
}

lcd3 is working (obviously), but I still get consecutively "X=0.0000". My worry is that if I need to use X somewhere else, it will not be there with the correct value - how should I deal with that?

Thanks for your help so far though!

X-man

vonCZ
27th September 2007, 16:09
whatup X-

try:




qDebug("X = %d",X);



your "X" variable is an int

jpn
27th September 2007, 17:05
With

qDebug() << X;
there would be no such problem.. ;)

X-man
28th September 2007, 01:27
You are both right - i accidentally fixed when I turner all the v's to floats. So now everything works like a charm...

...till everything stops working again.

Thanks for your replies, (unfortunately) I will get back with more questions :)

X-man