PDA

View Full Version : label setText value is 0



babygal
13th July 2010, 11:26
spinbox reads value input by user. then the value is multiplied with float value .and
displayed as output in a label. but the value is 0(incorrect).

Code:


QDoubleSpinBox *myspinBox;
QLabel *labelText;


QObject::connect(myspinBox,SIGNAL(valueChanged(QSt ring)),this,SLOT(setnewText(QString)));

void myDialog::setnewText(QString newinput)
{
newinputvalue = newinput;
multiplication();

}

void myDialog::multiplication()
{
float floatnum ;
float floatmultiplywith;
bool ok;

floatnum = newinputvalue .toFloat(&ok);

QString newValue = QString::number(floatmultiplywith*floatnum );
labelText->setText(newValue );

}

Lykurg
13th July 2010, 11:33
floatmultiplywith is not defined (= 0 as default I guess...), and did you check if your conversion to float works?

babygal
15th July 2010, 08:47
floatmultiplywith is not defined (= 0 as default I guess...), and did you check if your conversion to float works?

Yes . floatmultiplywith is not defined.

I removed the line as below and the conversion is correct:


labelText->setSuffix(" slices");

So now after removing the line of code as above the labelText value is correct!

ChrisW67
15th July 2010, 08:58
What did you do to give floatmultiplywith a value?

You are using a QSpinBox (QDoubleSpinBox) which will give an int (or double) when value() is called regardless of the prefix or suffix settings. By grabbing the text() value you get a string including the prefix/suffix when you really want the value. Calling cleanText() would be closer but you still have to manually convert to int/double. In short, if you want a number don't ask for the value as a string.