PDA

View Full Version : QVariant canConvert<>() issue



nikhilqt
9th June 2010, 13:51
Hi,

I have a issue regarding canConvert<>() template function. Consider the following example,


QVariant varConvertToDouble;
varConvertToDouble.clear();
varConvertToDouble = ASD28E+01;

bool bCheck = varConvertToDouble.canConvert<double>(); // Here bCheck is 'true'
bool bCheck2 = varConvertToDouble.canConvert(QVariant::Double); // Here bCheck2 is 'true'

bool* bValid = new bool();
varConvertToDouble.toDouble(bValid); // Here *bValid is 'false'

As a result the 2 APIs of QVariant are contradicting to each other. Could anybody please throw light on this?

PS: I have found a similar thread http://www.qtcentre.org/threads/19218-QVariant-question?highlight=canconvert. But there is no confirming answer to this.

Thanks,
Nikhil

SixDegrees
9th June 2010, 14:03
I don't see the problem. canConvert() tells you it will be able to perform the conversion from double to double; that isn't surprising. Then when you perform a conversion from double to bool, it is able to perform that conversion as well - with the resulting bool being false. Why is this an issue?

There's a difference between being able to perform a conversion and the result of the conversion.

nikhilqt
9th June 2010, 14:18
I don't see the problem. canConvert() tells you it will be able to perform the conversion from double to double; that isn't surprising.

Could you please see the value 'ASD28E+01', which is not a double. It is a invalid double value. canConvert<>() should also cannot
be able to convert this value as this is invalid, right?



There's a difference between being able to perform a conversion and the result of the conversion.

canConvert<>() method given for user convenience to check whether it can be converted before being really converting to
double. That is main use of it. If the results won't match then it would be misleading.

double to bool, sometimes is a standard conversion. which is not suitable example in this case i think.

nikhilqt
9th June 2010, 15:20
Hi,

Please update me if something is there regarding this problem. Thank you.

wysota
9th June 2010, 18:09
"canConvert" doesn't even look at the content, it only checks whether a particular data type (stored in the variant) can be cast to another. toDouble() also takes the value into consideration - it actually tries to convert the contents into the form you ask it to.

So for instance if you call canConvert on a string asking for conversion to double, it will tell you the conversion is possible. But if you actually perform the conversion, it might occur a particular string can (i.e. "123") or cannot (i.e. "abc") be converted to a double.

nikhilqt
10th June 2010, 14:19
Thanks. So the best option is to convert and check for the result, right?. Please let me know for what values canConvert<>() returns false, taking .toDouble() as example. I think, if the values are too extremities then it returns false(i.e., *&^%^*), right?

wysota
10th June 2010, 14:47
See QVariant::canConvert docs for the table of available conversions.