[Interest] QVariant toBool() not working correctly?

Kevin André hyperquantum at gmail.com
Tue Jan 26 16:52:15 CET 2021


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".
I am using Qt version 5.12.3.
So is this a mismatch between the Qt version of the docs and the Qt
version that I am using? Or am I missing something else here?


Thanks,

Kevin


More information about the Interest mailing list