Results 1 to 7 of 7

Thread: QVariant canConvert<>() issue

  1. #1
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QVariant canConvert<>() issue

    Hi,

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

    Qt Code:
    1. QVariant varConvertToDouble;
    2. varConvertToDouble.clear();
    3. varConvertToDouble = ASD28E+01;
    4.  
    5. bool bCheck = varConvertToDouble.canConvert<double>(); // Here bCheck is 'true'
    6. bool bCheck2 = varConvertToDouble.canConvert(QVariant::Double); // Here bCheck2 is 'true'
    7.  
    8. bool* bValid = new bool();
    9. varConvertToDouble.toDouble(bValid); // Here *bValid is 'false'
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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.

  3. The following user says thank you to SixDegrees for this useful post:

    nikhilqt (10th June 2010)

  4. #3
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QVariant canConvert<>() issue

    Quote Originally Posted by SixDegrees View Post
    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 View Post
    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.

  5. #4
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QVariant canConvert<>() issue

    Hi,

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

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    nikhilqt (10th June 2010)

  8. #6
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default 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?

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QVariant canConvert<>() issue

    See QVariant::canConvert docs for the table of available conversions.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 4th December 2009, 18:03
  2. VARIANT <-> QVariant
    By will49 in forum Qt Programming
    Replies: 2
    Last Post: 27th September 2009, 00:39
  3. a question about QVariant
    By calmspeaker in forum Qt Programming
    Replies: 4
    Last Post: 14th August 2009, 08:42
  4. QVariant to QAxObject*
    By QDrow in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2008, 10:35
  5. QTextCharFormat and QVariant
    By fullmetalcoder in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2007, 12:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.