Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QVariant.canConvert inconsistencies
Forum Updated to NodeBB v4.3 + New Features

QVariant.canConvert inconsistencies

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    an_ky
    wrote on last edited by
    #1

    Hi there,

    following minimized problem:

    @
    #include <QtCore/QCoreApplication>
    #include <QVariant>
    #include <QDebug>
    #include <stdexcept>

    template <typename Type>
    Type get(QVariant& source)
    {
    if(source.canConvert<Type>())
    return source.value<Type>();
    else
    throw std::runtime_error("Invalid type");
    }

    int main(int argc, char *argv[])
    {
    QVariant variant;
    variant.setValue(QString("blabla"));

    qDebug() << get<double>(variant); // "0"

    }
    @

    Obviously, QVariant.canConvert returns true and the string "blabla" gets converted into a double with the value 0 (zero) as the application does not throw an exception. Storing and/or querying other types result in the same value, better say the same "problem" because it seems that nearly any type can be converted into any other type but the return value my be zero (I write nearly because we haven't checked all types)

    Why?
    And is there a workaround which gives us a template based posibility to check the stored type.
    We used boost::lexical_cast before switching to QVariant as we through that QVariant would be the easier solution.

    greets.
    an ky

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      canConvert makes a static check, whether the type can be converted, not if the conversion can really be done. nearly everything can be converted to/from a string.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        an_ky
        wrote on last edited by
        #3

        Hi Gerolf,

        is there a way to do dynamic checks?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          There is also "type":http://doc.qt.nokia.com/4.7/qvariant.html#type for checks available.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            AFAIK, QVariant does not offer them.
            You have to do them on your own.

            But it will be tricky, becaus:

            Is a date convertable to a long? yes --> seconds since..., or no ?

            is a double convertable to an int, if it contains no int values, like 2.567 ?

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved