Add basic conversion functions from QVariant(QJsonValue).
There is a mismatch how QML and C++ converts QJsonValue. This patch unifies conversions by adding QJsonValue support in QVariant::convert(). Change-Id: I8a1db3d77c517945ef48064b4b66ba03aa4f2fd0 Reviewed-by: Frederik Gladhorn <frederik.gladhorn@digia.com>
This commit is contained in:
parent
af2deb85ca
commit
d57d184b6d
@ -162,6 +162,10 @@ static qlonglong qMetaTypeNumber(const QVariant::Private *d)
|
||||
return qRound64(d->data.f);
|
||||
case QVariant::Double:
|
||||
return qRound64(d->data.d);
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QJsonValue:
|
||||
return v_cast<QJsonValue>(d)->toDouble();
|
||||
#endif
|
||||
}
|
||||
Q_ASSERT(false);
|
||||
return 0;
|
||||
@ -206,12 +210,14 @@ static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
|
||||
case QMetaType::Long:
|
||||
case QMetaType::Float:
|
||||
case QMetaType::LongLong:
|
||||
case QMetaType::QJsonValue:
|
||||
return qMetaTypeNumber(d);
|
||||
case QVariant::ULongLong:
|
||||
case QVariant::UInt:
|
||||
case QMetaType::UChar:
|
||||
case QMetaType::UShort:
|
||||
case QMetaType::ULong:
|
||||
|
||||
return qlonglong(qMetaTypeUNumber(d));
|
||||
}
|
||||
|
||||
@ -240,6 +246,7 @@ static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok)
|
||||
case QMetaType::Long:
|
||||
case QMetaType::Float:
|
||||
case QMetaType::LongLong:
|
||||
case QMetaType::QJsonValue:
|
||||
return qulonglong(qMetaTypeNumber(d));
|
||||
case QVariant::ULongLong:
|
||||
case QVariant::UInt:
|
||||
@ -340,6 +347,9 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
case QVariant::Url:
|
||||
*str = v_cast<QUrl>(d)->toString();
|
||||
break;
|
||||
case QMetaType::QJsonValue:
|
||||
*str = v_cast<QJsonValue>(d)->toString();
|
||||
break;
|
||||
#endif
|
||||
case QVariant::Uuid:
|
||||
*str = v_cast<QUuid>(d)->toString();
|
||||
@ -580,6 +590,11 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
case QMetaType::ULong:
|
||||
*b = qMetaTypeUNumber(d) != Q_UINT64_C(0);
|
||||
break;
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QJsonValue:
|
||||
*b = v_cast<QJsonValue>(d)->toBool();
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
*b = false;
|
||||
return false;
|
||||
@ -616,6 +631,11 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
case QMetaType::ULong:
|
||||
*f = double(qMetaTypeUNumber(d));
|
||||
break;
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QJsonValue:
|
||||
*f = v_cast<QJsonValue>(d)->toDouble();
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
*f = 0.0;
|
||||
return false;
|
||||
@ -652,6 +672,11 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
|
||||
case QMetaType::ULong:
|
||||
*f = float(qMetaTypeUNumber(d));
|
||||
break;
|
||||
#ifndef QT_BOOTSTRAPPED
|
||||
case QMetaType::QJsonValue:
|
||||
*f = v_cast<QJsonValue>(d)->toDouble();
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
*f = 0.0f;
|
||||
return false;
|
||||
@ -2731,6 +2756,29 @@ bool QVariant::canConvert(int targetTypeId) const
|
||||
if (targetTypeId >= QMetaType::User)
|
||||
return canConvertMetaObject(currentType, targetTypeId, d.data.o);
|
||||
|
||||
if (currentType == QMetaType::QJsonValue) {
|
||||
switch (targetTypeId) {
|
||||
case QMetaType::QString:
|
||||
case QMetaType::Bool:
|
||||
case QMetaType::Int:
|
||||
case QMetaType::UInt:
|
||||
case QMetaType::Double:
|
||||
case QMetaType::Float:
|
||||
case QMetaType::ULong:
|
||||
case QMetaType::Long:
|
||||
case QMetaType::LongLong:
|
||||
case QMetaType::ULongLong:
|
||||
case QMetaType::UShort:
|
||||
case QMetaType::UChar:
|
||||
case QMetaType::Char:
|
||||
case QMetaType::SChar:
|
||||
case QMetaType::Short:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME It should be LastCoreType intead of Uuid
|
||||
if (currentType > int(QMetaType::QUuid) || targetTypeId > int(QMetaType::QUuid)) {
|
||||
switch (uint(targetTypeId)) {
|
||||
|
@ -142,6 +142,9 @@ private slots:
|
||||
void toDouble_data();
|
||||
void toDouble();
|
||||
|
||||
void toFloat_data();
|
||||
void toFloat();
|
||||
|
||||
void toPointF_data();
|
||||
void toPointF();
|
||||
|
||||
@ -456,6 +459,9 @@ void tst_QVariant::canConvert_data()
|
||||
var = QVariant::fromValue<signed char>(-1);
|
||||
QTest::newRow("SChar")
|
||||
<< var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
|
||||
var = QVariant::fromValue<QJsonValue>(QJsonValue(QStringLiteral("hello")));
|
||||
QTest::newRow("JsonValue")
|
||||
<< var << N << N << Y << N << N << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y;
|
||||
|
||||
#undef N
|
||||
#undef Y
|
||||
@ -511,6 +517,7 @@ void tst_QVariant::toInt_data()
|
||||
bytearray[2] = '0';
|
||||
bytearray[3] = '0';
|
||||
QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true;
|
||||
QTest::newRow("QJsonValue") << QVariant(QJsonValue(321)) << 321 << true;
|
||||
}
|
||||
|
||||
void tst_QVariant::toInt()
|
||||
@ -557,6 +564,7 @@ void tst_QVariant::toUInt_data()
|
||||
bytearray[2] = '2';
|
||||
bytearray[3] = '1';
|
||||
QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true;
|
||||
QTest::newRow("QJsonValue") << QVariant(QJsonValue(321)) << (uint)321 << true;
|
||||
}
|
||||
|
||||
void tst_QVariant::toUInt()
|
||||
@ -742,6 +750,8 @@ void tst_QVariant::toBool_data()
|
||||
QTest::newRow( "ulonglong1" ) << QVariant( (qulonglong)1 ) << true;
|
||||
QTest::newRow( "QChar" ) << QVariant(QChar('a')) << true;
|
||||
QTest::newRow( "Null_QChar" ) << QVariant(QChar(0)) << false;
|
||||
QTest::newRow("QJsonValue(true)") << QVariant(QJsonValue(true)) << true;
|
||||
QTest::newRow("QJsonValue(false)") << QVariant(QJsonValue(false)) << false;
|
||||
}
|
||||
|
||||
void tst_QVariant::toBool()
|
||||
@ -805,6 +815,7 @@ void tst_QVariant::toDouble_data()
|
||||
bytearray[2] = '.';
|
||||
bytearray[3] = '1';
|
||||
QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true;
|
||||
QTest::newRow("QJsonValue") << QVariant(QJsonValue(32.1)) << 32.1 << true;
|
||||
}
|
||||
|
||||
void tst_QVariant::toDouble()
|
||||
@ -820,6 +831,34 @@ void tst_QVariant::toDouble()
|
||||
QVERIFY( ok == valueOK );
|
||||
}
|
||||
|
||||
void tst_QVariant::toFloat_data()
|
||||
{
|
||||
QTest::addColumn<QVariant>("value");
|
||||
QTest::addColumn<float>("result");
|
||||
QTest::addColumn<bool>("valueOK");
|
||||
|
||||
QByteArray bytearray(4, ' ');
|
||||
bytearray[0] = '3';
|
||||
bytearray[1] = '2';
|
||||
bytearray[2] = '.';
|
||||
bytearray[3] = '1';
|
||||
QTest::newRow("QByteArray") << QVariant(bytearray) << float(32.1) << true;
|
||||
QTest::newRow("QJsonValue") << QVariant(QJsonValue(32.1)) << float(32.1) << true;
|
||||
}
|
||||
|
||||
void tst_QVariant::toFloat()
|
||||
{
|
||||
QFETCH(QVariant, value );
|
||||
QFETCH(float, result);
|
||||
QFETCH(bool, valueOK);
|
||||
QVERIFY(value.isValid());
|
||||
QVERIFY(value.canConvert(QMetaType::Float));
|
||||
bool ok;
|
||||
float d = value.toFloat(&ok);
|
||||
QCOMPARE(d, result);
|
||||
QVERIFY(ok == valueOK);
|
||||
}
|
||||
|
||||
void tst_QVariant::toLongLong_data()
|
||||
{
|
||||
QTest::addColumn<QVariant>("value");
|
||||
@ -843,6 +882,7 @@ void tst_QVariant::toLongLong_data()
|
||||
bytearray[2] = '0';
|
||||
bytearray[3] = '0';
|
||||
QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true;
|
||||
QTest::newRow("QJsonValue") << QVariant(QJsonValue(321)) << (qlonglong)321 << true;
|
||||
}
|
||||
|
||||
void tst_QVariant::toLongLong()
|
||||
@ -887,6 +927,7 @@ void tst_QVariant::toULongLong_data()
|
||||
bytearray[2] = '0';
|
||||
bytearray[3] = '1';
|
||||
QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true;
|
||||
QTest::newRow("QJsonValue") << QVariant(QJsonValue(321)) << (qulonglong)321 << true;
|
||||
}
|
||||
|
||||
void tst_QVariant::toULongLong()
|
||||
@ -953,6 +994,7 @@ void tst_QVariant::toString_data()
|
||||
QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" );
|
||||
QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
|
||||
QString( "123456789012" );
|
||||
QTest::newRow("QJsonValue") << QVariant(QJsonValue(QString("hello"))) << QString("hello");
|
||||
}
|
||||
|
||||
void tst_QVariant::toString()
|
||||
|
Loading…
Reference in New Issue
Block a user