[Interest] QVariant toBool() not working correctly?

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Tue Jan 26 17:37:44 CET 2021


Hi,

Il 26/01/21 16:52, Kevin André ha scritto:
> 
> 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?

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

Therefore, work around the issue and do the conversion yourself. (An 
alternative would be using TINYINT to represent bools.)

HTH,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4329 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210126/ef7f19aa/attachment.bin>


More information about the Interest mailing list