[Qt-interest] QVariant problem

Christian Dähn daehn at asinteg.de
Fri Jan 8 08:47:42 CET 2010


Hi,

Am Fr 08.01.2010 08:24 schrieb Patrik Nylund <patrik.nylund at tke.fi>:
> Hi
> 
> I have a problem with QVariant, the following doesn't work...
> 
> bool bad;
> quint32 myUInt32;
> QVariant *myVariant;
> QString myString = "0x12ae45";
> 
> myVariant = new QVariant(myString);
> myUInt32 = myVariant->toUInt(&bad);                  // Shouldn't this 
> work? I get bad = false and myInt32 = 0

That doesn't work - because the QVariant just knows the QString,
but isn't so intelligent to automatically transform strings into
other objects - please note: strings aren't stored as simple char
arrays in QString! The QVariant just sees the QString class, which
itself contains pointers etc. to the QByteArray which contains the
chars.

Instead try to get the QString out of QVariant and then call .toUInt()

myVariant = new QVariant(myString);
myUInt32 = myVariant->toString().toUInt(&bad);

Best Regards,
Chris





More information about the Qt-interest-old mailing list