From d27d2b5d72acb2a3be55d0af9ded4cd126a5f031 Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Wed, 18 Nov 2020 16:35:20 +0100 Subject: [PATCH] QMetaProperty::typeName: use name from metatype Except for types marked as unresolved, we're doing it anyway - the only difference is that now we skip looking up the metatype by typeid. [ChangeLog][QMetaProperty][Important Behavior Change] QMetaProperty::typeName returns now always the same name as name() of the corresponding metatype. This can cause a change for enum properties which were not fully-qualified. Change-Id: I1f57743948b7262ac06095d3bbc838d620f6e481 Reviewed-by: Qt CI Bot Reviewed-by: Lars Knoll Reviewed-by: Ulf Hermann --- src/corelib/kernel/qmetaobject.cpp | 3 +++ src/corelib/kernel/qmetaobjectbuilder.cpp | 15 ++++++++------- tests/auto/corelib/kernel/qobject/tst_qobject.cpp | 6 +++--- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index aaf8142638..fe6b4075ef 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -2934,6 +2934,9 @@ const char *QMetaProperty::typeName() const { if (!mobj) return nullptr; + // TODO: can the metatype be invalid for dynamic metaobjects? + if (const auto mt = metaType(); mt.isValid()) + return mt.name(); return rawTypeNameFromTypeInfo(mobj, data.type()); } diff --git a/src/corelib/kernel/qmetaobjectbuilder.cpp b/src/corelib/kernel/qmetaobjectbuilder.cpp index fcf6200f1b..ad7a8ce4ab 100644 --- a/src/corelib/kernel/qmetaobjectbuilder.cpp +++ b/src/corelib/kernel/qmetaobjectbuilder.cpp @@ -1337,14 +1337,15 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf, // Output the properties in the class. Q_ASSERT(!buf || dataIndex == pmeta->propertyData); - for (const auto &prop : d->properties) { + for (QMetaPropertyBuilderPrivate &prop : d->properties) { int name = strings.enter(prop.name); - int typeInfo; - if (QtPrivate::isBuiltinType(prop.type)) - typeInfo = QMetaType::fromName(prop.type).id(); - else - typeInfo = IsUnresolvedType | strings.enter(prop.type); + // try to resolve the metatype again if it was unknown + if (!prop.metaType.isValid()) + prop.metaType = QMetaType::fromName(prop.type); + const int typeInfo = prop.metaType.isValid() + ? prop.metaType.id() + : IsUnresolvedType | strings.enter(prop.type); int flags = prop.flags; @@ -1437,7 +1438,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf, if (buf) { meta->d.metaTypes = types; for (const auto &prop : d->properties) { - QMetaType mt = QMetaType::fromName(prop.type); + QMetaType mt = prop.metaType; *types = reinterpret_cast(mt); types++; } diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index 5cf1f0e50f..0fcae68505 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -1933,7 +1933,7 @@ void tst_QObject::property() QVERIFY(mo->indexOfProperty("alpha") != -1); property = mo->property(mo->indexOfProperty("alpha")); QVERIFY(property.isEnumType()); - QCOMPARE(property.typeName(), "Alpha"); + QCOMPARE(property.typeName(), "PropertyObject::Alpha"); QCOMPARE(property.userType(), QMetaType::fromType().id()); QVariant var = object.property("alpha"); @@ -2015,7 +2015,7 @@ void tst_QObject::property() QVERIFY(mo->indexOfProperty("priority") != -1); property = mo->property(mo->indexOfProperty("priority")); QVERIFY(property.isEnumType()); - QCOMPARE(property.typeName(), "Priority"); + QCOMPARE(property.typeName(), "PropertyObject::Priority"); QCOMPARE(property.userType(), QMetaType::fromType().id()); var = object.property("priority"); @@ -2036,7 +2036,7 @@ void tst_QObject::property() QVERIFY(mo->indexOfProperty("priority") != -1); property = mo->property(mo->indexOfProperty("priority")); QVERIFY(property.isEnumType()); - QCOMPARE(property.typeName(), "Priority"); + QCOMPARE(property.typeName(), "PropertyObject::Priority"); QCOMPARE(property.type(), QVariant::UserType); QCOMPARE(property.userType(), priorityMetaTypeId);