[Interest] QVariant toBool() not working correctly?

Christian Ehrlicher Ch.Ehrlicher at gmx.de
Tue Jan 26 17:24:23 CET 2021


Am 26.01.2021 um 16:52 schrieb Kevin André:
> Hi,
>
> I am reading a boolean value from a database column with SQL type
> "BIT" in MySQL. But I discovered that when a zero is stored in that
> column, my program apparently interprets that zero value as a boolean
> true value. I traced the problem to the QVariant->bool conversion:
>
> bool Database::getBool(QVariant v, bool nullValue)
> {
>      qDebug() << "getBool called for:" << v;
>      if (v.isNull())
>          return nullValue;
>
>      qDebug() << "getBool will convert" << v << "to bool" << v.toBool();
>      return v.toBool();
> }
>
> When reading a zero BIT value from the database, this outputs:
>
> getBool called for: QVariant(QString, "\u0000")
> getBool will convert QVariant(QString, "\u0000") to bool true
>
> The docs of toBool() claim that toBool should return false for a
> string value of "0".
But your string is not "0" - it's "\0".


Christian


More information about the Interest mailing list