I'm having trouble understanding the behaviour of QVariant::canConvert
and QVariant::toDouble
. I'd expect that both of these would return false if the underlying variant data is, say, a QString
, but I'm getting different results as shown:
#include <QString>
#include <QVariant>
#include <QDebug>
int main(int argc, char *argv[])
{
QString not_a_number("foo");
QVariant variant(not_a_number);
auto can_convert_1 = variant.canConvert<double>();
auto can_convert_2 = false;
variant.toDouble(&can_convert_2);
qDebug() << can_convert_1 << can_convert_2; //prints: true false
}
The method canConvert
returns true whereas the toDouble
return false.
Can someone explain this behaviour please?
I'm using Qt 5.15.7 on Windows with Visual Studio 2019.