diff --git a/templates/lib/metatype.cpp b/templates/lib/metatype.cpp index 13dee4b9..a8a15949 100644 --- a/templates/lib/metatype.cpp +++ b/templates/lib/metatype.cpp @@ -173,6 +173,19 @@ QVariant Grantlee::MetaType::lookup(const QVariant &object, return QVariant(); } + auto mo = QMetaType::metaObjectForType(object.userType()); + if (mo) { + QMetaType mt(object.userType()); + if (mt.flags().testFlag(QMetaType::IsGadget)) { + const auto idx = mo->indexOfProperty(property.toUtf8().constData()); + if (idx < 0) { + return QVariant(); + } + const auto mp = mo->property(idx); + return mp.readOnGadget(&object); + } + } + return customTypes()->lookup(object, property); } diff --git a/templates/tests/testgenerictypes.cpp b/templates/tests/testgenerictypes.cpp index 1abc7217..61dd6567 100644 --- a/templates/tests/testgenerictypes.cpp +++ b/templates/tests/testgenerictypes.cpp @@ -75,6 +75,8 @@ private Q_SLOTS: void testUnregistered(); void testPointerNonQObject(); + void testQGadget(); + }; // class TestGenericTypes class Person @@ -784,5 +786,23 @@ void TestGenericTypes::testPointerNonQObject() delete p; } +class CustomGadget +{ + Q_GADGET + Q_PROPERTY(int fortyTwo READ fortyTwo) +public: + int fortyTwo() { return 42; } +}; + +void TestGenericTypes::testQGadget() +{ + CustomGadget g; + auto v = QVariant::fromValue(g); + + auto result = Grantlee::MetaType::lookup(v, QStringLiteral("fortyTwo")); + + QCOMPARE(result.value(), 42); +} + QTEST_MAIN(TestGenericTypes) #include "testgenerictypes.moc"