Skip to content

Commit

Permalink
Support reading properties from Q_GADGETS
Browse files Browse the repository at this point in the history
  • Loading branch information
steveire committed Jul 21, 2019
1 parent 9b42eab commit 20b62ee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions templates/lib/metatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
20 changes: 20 additions & 0 deletions templates/tests/testgenerictypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ private Q_SLOTS:

void testUnregistered();
void testPointerNonQObject();
void testQGadget();

}; // class TestGenericTypes

class Person
Expand Down Expand Up @@ -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<int>(), 42);
}

QTEST_MAIN(TestGenericTypes)
#include "testgenerictypes.moc"

0 comments on commit 20b62ee

Please sign in to comment.