[Interest] QVariant toBool() not working correctly?

Kevin André hyperquantum at gmail.com
Tue Jan 26 17:58:48 CET 2021


Hi,

On Tue, Jan 26, 2021 at 5:38 PM Giuseppe D'Angelo via Interest
<interest at qt-project.org> wrote:

> You've been bit by a combination of two things, one is Qt not supporting
> the BIT column type natively (that's QTBUG-21326), and mapping it to a
> string. The other that the string you're getting contains a NUL
> (U+0000), not a literal 0 (U+0030). When converting a QVariant
> containing a string to bool, Qt maps empty strings, the string "0" and
> the string "false" (in any case) to false and everything else to true:
>
> > https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/kernel/qvariant.cpp?h=5.12#n344

Thanks Christian and Giuseppe for your help.
I am now using the following workaround:

bool Database::getBool(QVariant v, bool nullValue)
{
    if (v.isNull())
        return nullValue;

    /* workaround for zero BIT value being loaded as "\0" instead of "0" */
    if (v == QVariant(QChar(0)))
        return false;

    return v.toBool();
}


Kevin


More information about the Interest mailing list