From: Jędrzej Nowacki Date: Mon, 30 Jan 2012 11:21:43 +0000 (+0100) Subject: Align QVariant::UserType and QMetaType::User X-Git-Tag: qt-v5.0.0-alpha1~1288 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=56265031b763736163f1a229a7e7e6b7aaec8a36;p=profile%2Fivi%2Fqtbase.git Align QVariant::UserType and QMetaType::User There is no point in keeping separate values which should mean the same. QVariant::UserType was used also to construct a valid, null QVariant, containing an instance of unknown custom type. The concept was strange and useless as there was no operation that could be done on such QVariant. Therefore it was dropped. Please note that the patch slightly changes behavior of different functions accepting a type id as parameter. Before QVariant::UserType was an invalid type from QMetaType perspective (id 127 was not assigned to any built-in type), but QMetaType::User points to the first registered custom type. Change-Id: I5c7d541a9affdcdacf53a4eda2272bdafaa87b71 Reviewed-by: Kent Hansen Reviewed-by: Lars Knoll Reviewed-by: João Abecasis Reviewed-by: Andrew Stanley-Jones Reviewed-by: Aaron Kennedy --- diff --git a/dist/changes-5.0.0 b/dist/changes-5.0.0 index fc5bebd..280b5d9 100644 --- a/dist/changes-5.0.0 +++ b/dist/changes-5.0.0 @@ -437,6 +437,13 @@ Qt for Windows CE cause an abort(). +- QVariant + + * Definition of QVariant::UserType changed. Currently it is the same as + QMetaType::User, which means that it points to the first registered custom + type, instead of a nonexistent type. + + - QMessageBox * The static function QMessageBox::question has changed the default argument diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index 400fe54..7975fc2 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -2243,9 +2243,8 @@ QVariant QMetaProperty::read(const QObject *object) const t = QMetaType::type(typeName); if (t == QVariant::Invalid) t = QVariant::nameToType(typeName); - if (t == QVariant::Invalid || t == QVariant::UserType) { - if (t == QVariant::Invalid) - qWarning("QMetaProperty::read: Unable to handle unregistered datatype '%s' for property '%s::%s'", typeName, mobj->className(), name()); + if (t == QVariant::Invalid) { + qWarning("QMetaProperty::read: Unable to handle unregistered datatype '%s' for property '%s::%s'", typeName, mobj->className(), name()); return QVariant(); } } diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index cfaf22c..4197fe9 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -1608,7 +1608,7 @@ QVariant::Type QVariant::nameToType(const char *name) return Invalid; int metaType = QMetaType::type(name); - return metaType <= int(LastGuiType) ? QVariant::Type(metaType) : UserType; + return metaType <= int(UserType) ? QVariant::Type(metaType) : UserType; } #ifndef QT_NO_DATASTREAM @@ -1670,7 +1670,9 @@ void QVariant::load(QDataStream &s) return; typeId = mapIdFromQt3ToCurrent[typeId]; } else if (s.version() < QDataStream::Qt_5_0) { - if (typeId >= 128 && typeId != QVariant::UserType) { + if (typeId == 127 /* QVariant::UserType */) { + typeId = QMetaType::User; + } else if (typeId >= 128 && typeId != QVariant::UserType) { // In Qt4 id == 128 was FirstExtCoreType. In Qt5 ExtCoreTypes set was merged to CoreTypes // by moving all ids down by 97. typeId -= 97; @@ -1741,7 +1743,9 @@ void QVariant::save(QDataStream &s) const return; } } else if (s.version() < QDataStream::Qt_5_0) { - if (typeId >= 128 - 97 && typeId <= LastCoreType) { + if (typeId == QMetaType::User) { + typeId = 127; // QVariant::UserType had this value in Qt4 + } else if (typeId >= 128 - 97 && typeId <= LastCoreType) { // In Qt4 id == 128 was FirstExtCoreType. In Qt5 ExtCoreTypes set was merged to CoreTypes // by moving all ids down by 97. typeId += 97; @@ -1762,7 +1766,7 @@ void QVariant::save(QDataStream &s) const s << typeId; if (s.version() >= QDataStream::Qt_4_2) s << qint8(d.is_null); - if (typeId == QVariant::UserType) { + if (d.type >= QVariant::UserType) { s << QMetaType::typeName(userType()); } diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 07ef4dc..b61b0cc 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -153,7 +153,7 @@ class Q_CORE_EXPORT QVariant Icon = QMetaType::QIcon, SizePolicy = QMetaType::QSizePolicy, - UserType = 127, + UserType = QMetaType::User, LastType = 0xffffffff // need this so that gcc >= 3.4 allocates 32 bits for Type }; diff --git a/src/corelib/kernel/qvariant_p.h b/src/corelib/kernel/qvariant_p.h index 7065bcf..30159f1 100644 --- a/src/corelib/kernel/qvariant_p.h +++ b/src/corelib/kernel/qvariant_p.h @@ -348,14 +348,6 @@ public: void delegate(const QMetaTypeSwitcher::UnknownType*) { - if (m_x->type == QVariant::UserType) { - // TODO get rid of it - // And yes! we can support historical magic, unkonwn/unconstructed user type isn't that - // awesome? this QVariant::isValid will be true! - m_x->is_null = !m_copy; - m_x->is_shared = false; - return; - } qWarning("Trying to construct an instance of an invalid type, type id: %i", m_x->type); m_x->type = QVariant::Invalid; } @@ -406,8 +398,6 @@ public: void delegate(const QMetaTypeSwitcher::UnknownType*) { - if (m_d->type == QVariant::UserType) - return; qWarning("Trying to destruct an instance of an invalid type, type id: %i", m_d->type); } // Ignore nonconstructible type @@ -454,10 +444,7 @@ public: void delegate(const QMetaTypeSwitcher::UnknownType*) { - if (m_d->type == QVariant::UserType) - m_debugStream.nospace() << "QVariant::UserType"; - else - qWarning("Trying to stream an instance of an invalid type, type id: %i", m_d->type); + qWarning("Trying to stream an instance of an invalid type, type id: %i", m_d->type); } void delegate(const void*) { diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index 7f95f68..4e7935b 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -1704,6 +1704,7 @@ void tst_QObject::property() QVERIFY(!property.isEnumType()); QCOMPARE(property.typeName(), "CustomType*"); QCOMPARE(property.type(), QVariant::UserType); + QCOMPARE(property.userType(), qMetaTypeId()); CustomType *customPointer = 0; QVariant customVariant = object.property("custom"); @@ -1718,6 +1719,7 @@ void tst_QObject::property() QVERIFY(property.isWritable()); QCOMPARE(property.typeName(), "CustomType*"); QCOMPARE(property.type(), QVariant::UserType); + QCOMPARE(property.userType(), qMetaTypeId()); QVERIFY(object.setProperty("custom", customVariant)); QCOMPARE(object.custom(), customPointer); diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 2bf554d..f7bdfd8 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -1842,10 +1842,6 @@ void tst_QVariant::operator_eq_eq_data() QTest::newRow("HashSecondLarger") << QVariant(hash1) << QVariant(hash2) << false; } - - QTest::newRow( "UserType" ) << QVariant(QVariant::UserType) << QVariant(QVariant::UserType) << true; - QVariant mUserType(QVariant::UserType); - QTest::newRow( "Shared UserType" ) << mUserType << mUserType << true; } void tst_QVariant::operator_eq_eq() @@ -1919,7 +1915,6 @@ void tst_QVariant::typeName_data() QTest::newRow("39") << int(QVariant::RectF) << QByteArray("QRectF"); QTest::newRow("40") << int(QVariant::PointF) << QByteArray("QPointF"); QTest::newRow("41") << int(QVariant::RegExp) << QByteArray("QRegExp"); - QTest::newRow("42") << int(QVariant::UserType) << QByteArray(); QTest::newRow("43") << int(QVariant::Matrix) << QByteArray("QMatrix"); QTest::newRow("44") << int(QVariant::Transform) << QByteArray("QTransform"); QTest::newRow("45") << int(QVariant::Hash) << QByteArray("QVariantHash"); @@ -2031,10 +2026,10 @@ void tst_QVariant::userType() qVariantSetValue(userVar, data); QCOMPARE(userVar.type(), QVariant::UserType); + QCOMPARE(userVar.userType(), qMetaTypeId()); QCOMPARE(userVar.typeName(), "MyType"); QVERIFY(!userVar.isNull()); QVERIFY(!userVar.canConvert(QVariant::String)); - QVERIFY(!userVar.canConvert(QVariant::UserType)); QVariant userVar2(userVar); QVERIFY(userVar == userVar2); @@ -2060,10 +2055,10 @@ void tst_QVariant::userType() qVariantSetValue(userVar, &data); QCOMPARE(userVar.type(), QVariant::UserType); + QCOMPARE(userVar.userType(), qMetaTypeId()); QCOMPARE(userVar.typeName(), "MyType*"); QVERIFY(!userVar.isNull()); QVERIFY(!userVar.canConvert(QVariant::String)); - QVERIFY(!userVar.canConvert(QVariant::UserType)); QVariant userVar2(userVar); QVERIFY(userVar == userVar2); @@ -2696,7 +2691,7 @@ Q_DECLARE_METATYPE( MyClass ) void tst_QVariant::loadUnknownUserType() { qRegisterMetaType("MyClass"); - char data[] = {0, 0, 0, 127, 0, 0, 0, 0, 8, 77, 121, 67, 108, 97, 115, 115, 0}; + char data[] = {0, 0, 1, 0, 0, 0, 0, 0, 8, 77, 121, 67, 108, 97, 115, 115, 0}; QByteArray ba(data, sizeof(data)); QDataStream ds(&ba, QIODevice::ReadOnly); @@ -3306,6 +3301,7 @@ void tst_QVariant::movabilityTest() memcpy(buffer, &variant, sizeof(QVariant)); QCOMPARE(buffer[0].type(), QVariant::UserType); + QCOMPARE(buffer[0].userType(), qMetaTypeId()); MyNotMovable tmp(buffer[0].value()); new (&variant) QVariant(); diff --git a/tests/auto/dbus/qdbusmarshall/common.h b/tests/auto/dbus/qdbusmarshall/common.h index d49f4ef..87c434b 100644 --- a/tests/auto/dbus/qdbusmarshall/common.h +++ b/tests/auto/dbus/qdbusmarshall/common.h @@ -610,10 +610,6 @@ template<> bool compare(const QVariant &v1, const QVariant &v2) else if (id == QVariant::ByteArray) return compare(v1.toByteArray(), v2.toByteArray()); - else if (id < int(QVariant::UserType)) // yes, v1.type() - // QVariant can compare - return v1 == v2; - else if (id == QMetaType::UChar) return qvariant_cast(v1) == qvariant_cast(v2); @@ -716,6 +712,10 @@ template<> bool compare(const QVariant &v1, const QVariant &v2) else if (id == qMetaTypeId()) // (is) return qvariant_cast(v1) == qvariant_cast(v2); + else if (id < int(QVariant::UserType)) // yes, v1.type() + // QVariant can compare + return v1 == v2; + else { qWarning() << "Please write a comparison case for type" << v1.typeName(); return false; // unknown type