From 22943b3a3f44fcf67da84f027d898ee9f2dfb666 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 23 Mar 2023 07:02:42 -1000 Subject: [PATCH] QVariant: ensure the type custom is registered on construction I must have broken this in the 6.5 work I did for QMetaType and QVariant, but I haven't searched which commit exactly did it. Our QVariant tests are old and thus only checked the type ID, which meant that they caused the registration by the act of asking for the ID in the first place; this commit adds a couple of explicit checks for the type registered by name before the ID. Fixes: QTBUG-112205 Change-Id: Idd5e1bb52be047d7b4fffffd174f1b14d90fd7a3 Reviewed-by: Fabian Kosmale (cherry picked from commit 8570e86fff183ffa4c1dff577c7fa14e3342daee) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qvariant.cpp | 1 + tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index c41f62e326f..808dd43f080 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -874,6 +874,7 @@ QVariant::QVariant(const QVariant &p) */ QVariant::QVariant(QMetaType type, const void *copy) : d(type.iface()) { + type.registerType(); if (isValidMetaTypeForVariant(type.iface(), copy)) customConstruct(type.iface(), &d, copy); else diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 9cf44ec76b6..61a8e761564 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -1995,6 +1995,8 @@ void tst_QVariant::userType() QVariant userVar; userVar.setValue(data); + QVERIFY(QMetaType::fromName("MyType").isValid()); + QCOMPARE(QMetaType::fromName("MyType"), QMetaType::fromType()); QVERIFY(userVar.typeId() > QMetaType::User); QCOMPARE(userVar.userType(), qMetaTypeId()); QCOMPARE(userVar.typeName(), "MyType"); @@ -2118,7 +2120,15 @@ void tst_QVariant::podUserType() pod.a = 10; pod.b = 20; + // one of these two must register the type + // (QVariant::fromValue calls QMetaType::fromType) QVariant pod_as_variant = QVariant::fromValue(pod); + QMetaType mt = QMetaType::fromType(); + QCOMPARE(pod_as_variant.metaType(), mt); + QCOMPARE(pod_as_variant.metaType().name(), mt.name()); + QCOMPARE(QMetaType::fromName(mt.name()), mt); + QCOMPARE_NE(pod_as_variant.typeId(), 0); + MyTypePOD pod2 = qvariant_cast(pod_as_variant); QCOMPARE(pod.a, pod2.a);