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. Q_PROPERTY and null QVariant
Forum Updated to NodeBB v4.3 + New Features

Q_PROPERTY and null QVariant

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 8.4k 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.
  • G Offline
    G Offline
    goetz
    wrote on last edited by
    #2

    setProperty would eventually call convert() on the QVariant. The conversion always fails for a null QVariant.

    http://www.catb.org/~esr/faqs/smart-questions.html

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p-himik
      wrote on last edited by
      #3

      Thanks for the answer.
      Documentation says:

      bq. bool QVariant::convert ( Type t )
      Casts the variant to the requested type, t. If the cast cannot be done, the variant is cleared. Returns true if the current type of the variant was successfully cast; otherwise returns false.
      Warning: For historical reasons, converting a null QVariant results in a null value of the desired type (e.g., an empty string for QString) and a result of false.

      As I can understand from warning setProperty should set property to a null value of the desired type (and I understand it as a default-constructed value) when the QVariant is null.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #4

        I think the behavior is correct, because there is no such thing as a default constructed boolean. There is no standard value for a boolean (which is why you need to initialize it yourself explicitly).

        Considder this:
        @
        bool test;
        qDebug() << test;
        @
        Will the output be 'true' or 'false'? There is no way to tell.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p-himik
          wrote on last edited by
          #5

          Sorry I haven't wrote it before: I meant "these":http://qt-project.org/doc/qt-4.8/containers.html#default-constructed-values default-constructed values.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #6

            That documentation refers to the container classes. QVariant isn't such a container class.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              p-himik
              wrote on last edited by
              #7

              Yes, I know. But "this":http://qt-project.org/doc/qt-4.8/qvariant.html#value says about default-constructed values as well.
              I think it's strange that QVariant::value() returnes default-constructed value if this QVariant is null but QObject::setProperty() returnes false. As I think either QVariant::value() should accept a pointer to a bool (as QVariant::toInt(bool *)) or QObject::setProperty() should set property to a default-constructed value if QVariant is null.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #8

                You are right, and it is weird that the QVariant documentation actually points to the documentation for the containers on this. I did not see that before. Sorry, I think you'd best talk with someone more into the details of QVariant than I am. Perhaps you can discuss this on IRC in the #qt-labs channel (freenode network).

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  p-himik
                  wrote on last edited by
                  #9

                  Thanks for advice!

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #10

                    The Qt meta object system tries to convert the QVariant passed to setProperty() to the type of the property. So, what's called in the end is

                    @
                    QVariant v;
                    bool ok = v.convert(QVariant::Bool);
                    if(!ok)
                    return false; // from setProperty()
                    @

                    Thus, if the QVariant passed in cannot be converted to the type of the property, the setProperty call will fail eventually. Also note that QVariant::convert() will return false in case you pass it a Null QVariant - which is the case here:

                    @
                    QVariant v1 = QString(); // null - convert fails

                    QVariant v2 = QString(""); // NOT null, convert succeeds
                    QVariant v3 = ""; // NOT null, convert succeeds
                    @

                    Alltogether, this is good™. You don't want to set a bool property from a QColor or a QVariantMap value, but get an error condition in theses cases.

                    Also, QVariant::value() is not involved in this game at all. Thus, applying its documentation to setProperty() is invalid.

                    References:

                    • Implementation of QObject::setProperty()
                    • Implementation of QMetaProperty::write()
                    • API docs of QVariant::convert()

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      p-himik
                      wrote on last edited by
                      #11

                      Thanks, Volker. I've read that "For historical reasons, converting a null QVariant results in a null value of the desired type (e.g., an empty string for QString) and a result of false." and now everything is clear.
                      Though I don't like that historical reasons. They forced me to write another three lines of code :)

                      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