diff --git a/include/core/converter.h b/include/core/converter.h index 3833e0d6..1ac4bd1a 100644 --- a/include/core/converter.h +++ b/include/core/converter.h @@ -70,13 +70,17 @@ struct DefaultConverter { if (!v.canConvert()) { if (errors) errors->append(QString("Can't convert from JSON to type '%1'").arg(v.typeName())); } else { +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) // 'canConvert' is only true if a conversion between types is theoretically possible, // not necessarily if this value can be converted. e.g. "2" can be converted to int (2), // but "hello world" cannot be converted to int and becomes 0. + // For older versions of Qt, we rely on QVariant::value. + T value; bool ok = QMetaType::convert(v.metaType(), v.constData(), QMetaType::fromType(), &value); if (ok) return value; else if (errors) errors->append(QString("Failed to convert JSON value to type '%1'").arg(v.typeName())); +#endif } // For failed conversion, return a default constructed value. return v.value();