From 05fd01c14a7a4b26f366704fb0b0a4eddaceccf4 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Wed, 7 Jan 2015 11:51:37 +0100 Subject: Fix property access and method invocation on value types that use inheritance For gadgets/value types we use moc's static_metacall, which doesn't call the parent class implementation. Therefore before placing a static metacall we must resolve the indicies and find the right meta-object. Change-Id: I258e3d9ecfc704498c68772dc42b16134a3bfd83 Reviewed-by: Olivier Goffart Reviewed-by: Alex Blasche --- .../qml/qqmlvaluetypes/data/customvaluetype.qml | 3 ++ .../auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp | 54 ++++++++++++++++++++++ 2 files changed, 57 insertions(+) (limited to 'tests/auto/qml/qqmlvaluetypes') diff --git a/tests/auto/qml/qqmlvaluetypes/data/customvaluetype.qml b/tests/auto/qml/qqmlvaluetypes/data/customvaluetype.qml index 7ae73475c3..380eb55f54 100644 --- a/tests/auto/qml/qqmlvaluetypes/data/customvaluetype.qml +++ b/tests/auto/qml/qqmlvaluetypes/data/customvaluetype.qml @@ -5,4 +5,7 @@ TypeWithCustomValueType { desk { monitorCount: 3 } + derivedGadget { + baseProperty: 42 + } } diff --git a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp index f07a34b2f2..45cbbebdc3 100644 --- a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp +++ b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp @@ -90,6 +90,7 @@ private slots: void groupedInterceptors_data(); void customValueType(); void customValueTypeInQml(); + void gadgetInheritance(); private: QQmlEngine engine; @@ -1484,13 +1485,48 @@ void tst_qqmlvaluetypes::customValueType() QCOMPARE(cppOffice.desk().monitorCount, 2); } +struct BaseGadget +{ + Q_GADGET + Q_PROPERTY(int baseProperty READ baseProperty WRITE setBaseProperty) +public: + BaseGadget() : m_baseProperty(0) {} + + int baseProperty() const { return m_baseProperty; } + void setBaseProperty(int value) { m_baseProperty = value; } + int m_baseProperty; + + Q_INVOKABLE void functionInBaseGadget(int value) { m_baseProperty = value; } +}; + +Q_DECLARE_METATYPE(BaseGadget) + +struct DerivedGadget : public BaseGadget +{ + Q_GADGET + Q_PROPERTY(int derivedProperty READ derivedProperty WRITE setDerivedProperty) +public: + DerivedGadget() : m_derivedProperty(0) {} + + int derivedProperty() const { return m_derivedProperty; } + void setDerivedProperty(int value) { m_derivedProperty = value; } + int m_derivedProperty; + + Q_INVOKABLE void functionInDerivedGadget(int value) { m_derivedProperty = value; } +}; + class TypeWithCustomValueType : public QObject { Q_OBJECT Q_PROPERTY(MyDesk desk MEMBER m_desk) + Q_PROPERTY(DerivedGadget derivedGadget READ derivedGadget WRITE setDerivedGadget) public: MyDesk m_desk; + + DerivedGadget derivedGadget() const { return m_derivedGadget; } + void setDerivedGadget(const DerivedGadget &value) { m_derivedGadget = value; } + DerivedGadget m_derivedGadget; }; void tst_qqmlvaluetypes::customValueTypeInQml() @@ -1503,6 +1539,24 @@ void tst_qqmlvaluetypes::customValueTypeInQml() TypeWithCustomValueType *t = qobject_cast(object.data()); Q_ASSERT(t); QCOMPARE(t->m_desk.monitorCount, 3); + QCOMPARE(t->m_derivedGadget.baseProperty(), 42); +} + +Q_DECLARE_METATYPE(DerivedGadget) + +void tst_qqmlvaluetypes::gadgetInheritance() +{ + QJSEngine engine; + + QJSValue value = engine.toScriptValue(DerivedGadget()); + + QCOMPARE(value.property("baseProperty").toInt(), 0); + value.setProperty("baseProperty", 10); + QCOMPARE(value.property("baseProperty").toInt(), 10); + + QJSValue method = value.property("functionInBaseGadget"); + method.call(QJSValueList() << QJSValue(42)); + QCOMPARE(value.property("baseProperty").toInt(), 42); } QTEST_MAIN(tst_qqmlvaluetypes) -- cgit v1.2.3