From fd45278eaa3004e5450f011102fe0d55ae6ed8b3 Mon Sep 17 00:00:00 2001 From: Mikolaj Boc Date: Thu, 7 Jul 2022 11:32:01 +0200 Subject: [PATCH] Specialize MetaObjectForType for non-pointer QObject-derived types QMetaTypeInterfaceWrapper tries to find the metaObjectFunction using the MetaObjectForType template. Using SFINAE, for a QObject, it should resolve to a suitable specialization. Such a specialization doesn't yet exist. It had to be created. The following path returns nullptr for registered meta types: auto metatype = QMetaType(typeId); requestedTestType.metaObject() -> returns nullptr since a bad template argument is fed to MetaObjectForType in QMetaTypeInterfaceWrapper::metaType's static initializer. Change-Id: I8b31c51e12cb19c333e00480b0177354b910ce1e Reviewed-by: Thiago Macieira --- src/corelib/kernel/qmetatype.h | 7 ++++++- tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 2e97e1892d..24d1de4a28 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -901,7 +901,12 @@ namespace QtPrivate static constexpr const QMetaObject *metaObjectFunction(const QMetaTypeInterface *) { return &T::staticMetaObject; } }; template - struct MetaObjectForType::IsGadgetOrDerivedFrom>::type> + struct MetaObjectForType::IsGadgetOrDerivedFrom>, + std::is_base_of + > + >> { static constexpr const QMetaObject *value() { return &T::staticMetaObject; } static constexpr const QMetaObject *metaObjectFunction(const QMetaTypeInterface *) { return &T::staticMetaObject; } diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index ed413107c0..053408997c 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -1999,6 +1999,7 @@ void tst_QMetaType::metaObject_data() QTest::newRow("unknown-type") << int(QMetaType::UnknownType) << static_cast(0) << false << false << false; QTest::newRow("QObject") << int(QMetaType::QObjectStar) << &QObject::staticMetaObject << false << false << true; QTest::newRow("QFile*") << ::qMetaTypeId() << &QFile::staticMetaObject << false << false << true; + QTest::newRow("MyObject") << ::qMetaTypeId() << &MyObject::staticMetaObject << false << false << false; QTest::newRow("MyObject*") << ::qMetaTypeId() << &MyObject::staticMetaObject << false << false << true; QTest::newRow("int") << int(QMetaType::Int) << static_cast(0) << false << false << false; QTest::newRow("QEasingCurve") << ::qMetaTypeId() << &QEasingCurve::staticMetaObject << true << false << false;