QVariant - empty or Null
-
wrote on 27 Apr 2023, 11:33 last edited by MasterQ
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()){}
-
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()){}
wrote on 27 Apr 2023, 11:51 last edited by@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 emptyQVariant
so it should be distinct. Otherwise you would have to invent some uniqueQVariant
forNULL
.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, useQVariant(QMetaType::fromType<QString>())
if you are binding a string.This is still null
QVariant
, soQVariant::isNull()
can still be used to test for it. -
@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 emptyQVariant
so it should be distinct. Otherwise you would have to invent some uniqueQVariant
forNULL
.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, useQVariant(QMetaType::fromType<QString>())
if you are binding a string.This is still null
QVariant
, soQVariant::isNull()
can still be used to test for it.@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.
1/3