From c4cea61cea7c78e093bae68a14a790fa25d7b461 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 19 Jul 2022 11:45:40 -0700 Subject: [PATCH] QMetaType: fix isRegistered() It's not registered until an ID is assigned. Change-Id: I3859764fed084846bcb0fffd17034f5b369c5b4d Reviewed-by: Fabian Kosmale (cherry picked from commit b1d9331c156b7ff5c724600eee21ac50f4565868) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qmetatype.cpp | 11 ++++++++--- tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp | 9 +++++++++ tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index 7ab196ddd9e..92ad38613b7 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -465,6 +465,8 @@ const char *QtMetaTypePrivate::typedefNameForType(const QtPrivate::QMetaTypeInte Returns \c true if this QMetaType object contains valid information about a type, false otherwise. + + \sa isRegistered() */ bool QMetaType::isValid() const { @@ -475,12 +477,15 @@ bool QMetaType::isValid() const \fn bool QMetaType::isRegistered() const \since 5.0 - Returns \c true if this QMetaType object contains valid - information about a type, false otherwise. + Returns \c true if this QMetaType object has been registered with the Qt + global metatype registry. Registration allows the type to be found by its + name (using QMetaType::fromName()) or by its ID (using the constructor). + + \sa qRegisterMetaType(), isValid() */ bool QMetaType::isRegistered() const { - return d_ptr; + return d_ptr && d_ptr->typeId.loadRelaxed(); } /*! diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index 4202e9ca754..422a3221ca8 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -861,6 +861,7 @@ void tst_QMetaType::sizeOf() { QFETCH(int, type); QFETCH(size_t, size); + ignoreInvalidMetaTypeWarning(type); QCOMPARE(size_t(QMetaType::sizeOf(type)), size); } @@ -873,6 +874,7 @@ void tst_QMetaType::sizeOfStaticLess() { QFETCH(int, type); QFETCH(size_t, size); + ignoreInvalidMetaTypeWarning(type); QCOMPARE(size_t(QMetaType(type).sizeOf()), size); } @@ -909,6 +911,7 @@ void tst_QMetaType::alignOf() { QFETCH(int, type); QFETCH(size_t, size); + ignoreInvalidMetaTypeWarning(type); QCOMPARE(size_t(QMetaType(type).alignOf()), size); } @@ -1534,6 +1537,12 @@ void tst_QMetaType::isRegisteredStaticLess() QCOMPARE(QMetaType(typeId).isRegistered(), registered); } +struct NotARegisteredType {}; +void tst_QMetaType::isNotRegistered() +{ + QVERIFY(!QMetaType::fromType().isRegistered()); +} + typedef QHash IntUIntHash; Q_DECLARE_METATYPE(IntUIntHash) typedef QMap IntUIntMap; diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h index a30963a919e..37d9f958e9a 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h @@ -84,6 +84,7 @@ private slots: void isRegistered(); void isRegisteredStaticLess_data(); void isRegisteredStaticLess(); + void isNotRegistered(); void isEnum(); void automaticTemplateRegistration_1(); void automaticTemplateRegistration_2(); // defined in tst_qmetatype3.cpp