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::isNull() method (Qt 5.2)
Forum Update on Monday, May 27th 2025

QVariant::isNull() method (Qt 5.2)

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.0k Views
  • 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.
  • S Offline
    S Offline
    silicomancer
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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!

      Greetz, Jeroen

      1 Reply Last reply
      0
      • S Offline
        S Offline
        silicomancer
        wrote on last edited by
        #3

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

        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