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 - empty or Null
Forum Updated to NodeBB v4.3 + New Features

QVariant - empty or Null

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 3.6k 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.
  • M Offline
    M Offline
    MasterQ
    wrote on 27 Apr 2023, 11:33 last edited by MasterQ
    #1

    Hi,

    QVariant can hold a variety of different data types.

    Since I have to deal with null-values I get from or put to a database, I am wondering how such null-values should be handled in Qt.

    QVariant has member isNull but this is simply a check for null-pointer or if it is uninitialized.

    Null is not 0, it is a synomym for "nothing" or "empty" or how you may call it.

    Any idea how I set a QVariant to null? Or is the valid-Flag a better choice?

    Is it as simple as

    value = QVariant() // setting value to null
    
    if(value.IsNull()){} // asking if value is null
    if(value.IsValid()){}
    
    J 1 Reply Last reply 27 Apr 2023, 11:51
    0
    • M MasterQ
      27 Apr 2023, 11:33

      Hi,

      QVariant can hold a variety of different data types.

      Since I have to deal with null-values I get from or put to a database, I am wondering how such null-values should be handled in Qt.

      QVariant has member isNull but this is simply a check for null-pointer or if it is uninitialized.

      Null is not 0, it is a synomym for "nothing" or "empty" or how you may call it.

      Any idea how I set a QVariant to null? Or is the valid-Flag a better choice?

      Is it as simple as

      value = QVariant() // setting value to null
      
      if(value.IsNull()){} // asking if value is null
      if(value.IsValid()){}
      
      J Offline
      J Offline
      JonB
      wrote on 27 Apr 2023, 11:51 last edited by
      #2

      @MasterQ said in QVariant - empty or Null:

      value = QVariant() // setting value to null

      This is the best/simplest representation for a NULL returned from a database. No other value will return an empty QVariant so it should be distinct. Otherwise you would have to invent some unique QVariant for NULL.

      However, I think the Qt database routines want you to indicate the type of the "NULL", to match the database column type. You want your nulls to match what you have to pass to e.g. a QSqlQuery when sending them to the database. And QSqlQuery::addBindValue(const QVariant &val, QSql::ParamType paramType = QSql::In) says:

      To bind a NULL value, use a null QVariant; for example, use QVariant(QMetaType::fromType<QString>()) if you are binding a string.

      This is still null QVariant, so QVariant::isNull() can still be used to test for it.

      C 1 Reply Last reply 27 Apr 2023, 16:26
      0
      • J JonB
        27 Apr 2023, 11:51

        @MasterQ said in QVariant - empty or Null:

        value = QVariant() // setting value to null

        This is the best/simplest representation for a NULL returned from a database. No other value will return an empty QVariant so it should be distinct. Otherwise you would have to invent some unique QVariant for NULL.

        However, I think the Qt database routines want you to indicate the type of the "NULL", to match the database column type. You want your nulls to match what you have to pass to e.g. a QSqlQuery when sending them to the database. And QSqlQuery::addBindValue(const QVariant &val, QSql::ParamType paramType = QSql::In) says:

        To bind a NULL value, use a null QVariant; for example, use QVariant(QMetaType::fromType<QString>()) if you are binding a string.

        This is still null QVariant, so QVariant::isNull() can still be used to test for it.

        C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 27 Apr 2023, 16:26 last edited by
        #3

        @JonB said in QVariant - empty or Null:

        This is still null QVariant, so QVariant::isNull() can still be used to test for it.

        Correct. But QVariant::isValid() returns true in this case but not for value = QVariant() (as written in the documentation). But binding an invalid QVariant to a QSqlQuery will work too.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2

        1/3

        27 Apr 2023, 11:33

        • Login

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