QVariant canConvert<>() issue
Hi,
I have a issue regarding canConvert<>() template function. Consider the following example,
Code:
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/1921...ght=canconvert. But there is no confirming answer to this.
Thanks,
Nikhil
Re: QVariant canConvert<>() issue
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.
Re: QVariant canConvert<>() issue
Quote:
Originally Posted by
SixDegrees
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?
Quote:
Originally Posted by
SixDegrees
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.
Re: QVariant canConvert<>() issue
Hi,
Please update me if something is there regarding this problem. Thank you.
Re: QVariant canConvert<>() issue
"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.
Re: QVariant canConvert<>() issue
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?
Re: QVariant canConvert<>() issue
See QVariant::canConvert docs for the table of available conversions.