QVariant::isNull() method (Qt 5.2)

  • The documentation says:
    "Returns true if this is a null variant, false otherwise. A variant is considered null if it contains a default constructed value or a built-in type instance that has an isNull method, in which case the result would be the same as calling isNull on the wrapped object."

    This seems not correct to me. When creating a QVariant by "QVariant boolVariant = bool();" "boolVariant.isNull()" returns false. However according to the above description is should return true since it contains a default constructed value.

    Is the documentation misleading? How should it read? How does isNull() actually work?

  • Hi,
    You are mistaken with C and C++ IYAM.
    What you do, is create a QVariant with a default constructor and reassign the value to hold the value created via a default constructed bool. So the QVariant hold a VALUE, not a DEFAULT VALUE, thus returns false!
    You should test:
    QVariant boolVariant (bool());
    // or
    QVariant boolVariant;
    // NOT: THIS IS C code, NOT C++
    QVariant boolVariant = bool();
    This IS a default constructed QVariant!

  • "QVariant boolVariant = bool();" definitely calls the "QVariant::QVariant(bool)" constructor directly in C++. I used the debugger to verify this.

    Also line 3 of your example contructs an invalid variant (which is not the same as a null variant).

    It seems the only way to create a null-variant is something like "QVariant(QVariant::Int)" or by wrapping an object that has its own isNull() method. Still I have no idea why the documentation mentions default constructed values.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.