PDA

View Full Version : Conversion of Qstring to Qint64



citix
20th January 2014, 14:34
I have a LineEdit and want to convert the value to qint64 (long) for some calculation. But I always get a problem and the value return 0. How can I solve it?


inline long GetInteger64FromStatic(QLineEdit* lineEdit)
{
QString text;
qint64 nValue = 0;
//bool convertOK;
bool ok;

nValue = lineEdit->text().toLong(&ok,10);

return nValue;

}

anda_skoa
20th January 2014, 16:45
If you want a qint64 you should call QString::toLongLong().
long has the problem that on Windows, or at least the Microsoft C++ compiler, a long is only 4 bytes, basically equivalent of an int.

Cheers,
_

citix
21st January 2014, 07:36
when I try this code I still get 0 for value = 20.5. But I need value=20. How can I solve it?


inline qint64 GetInteger64FromStatic(QLineEdit* lineEdit)
{
QString text;
qint64 nValue = 0;
//bool convertOK;
bool ok;

nValue = lineEdit->text().toLongLong(&ok,10);

return nValue;

}

ChrisW67
21st January 2014, 08:32
Well, yes, 20.5 is not an integer so you get zero and the ok flag set to false. If you need to accept floating point numbers then you need to convert to a double and then apply whatever rounding is appropriate.

anda_skoa
21st January 2014, 08:35
20.5 is not an integer value, it contains a .

Instead this is a floating point value, so have a look at QString::toDouble()

Cheers,
_