diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2016-11-15 14:44:21 +0100 |
---|---|---|
committer | Erik Verbruggen <erik.verbruggen@qt.io> | 2016-11-16 09:01:25 +0000 |
commit | 12b7e6bfe5a8fb7666a929090483921c3f99ae8a (patch) | |
tree | 29eee12f334a9ed21c0db19e447018d88cf448a7 /tests/auto/qml/qqmlvaluetypes | |
parent | 4ef8b7e7a8a93b2921560db8cf021f89409154e5 (diff) |
Fix reading of enum properties from gadgets
QMetaProperty::type() maps an un-registered enum to QMetaType::Int, and
so if a property cache is created for a gadget with enum properties,
then their type will be int and we'll correctly read enum properties as
ints in JavaScript. However if the enum is registered at the time we
create the cache, then the property type will be the specific type and
not QMetaType::Int. The property reading code in QV4::QObjectWrapper can
deal with that, but the property reading code in the gadget value type
wrapper code did not.
[ChangeLog][Qt][Qml] Fix reading of enum properties from gadgets / value
types when the enum was registered with qRegisterMetaType().
Change-Id: I7812b216a276dcc95c36e313507e1a1142250d0b
Reviewed-by: Erik Verbruggen <erik.verbruggen@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmlvaluetypes')
-rw-r--r-- | tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp index 04abe0bfcb..e82df79acb 100644 --- a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp +++ b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp @@ -94,6 +94,7 @@ private slots: void customValueTypeInQml(); void gadgetInheritance(); void toStringConversion(); + void enumProperties(); private: QQmlEngine engine; @@ -1652,6 +1653,39 @@ void tst_qqmlvaluetypes::toStringConversion() QCOMPARE(stringConversion.toString(), StringLessGadget_to_QString(g)); } +struct GadgetWithEnum +{ + Q_GADGET +public: + + enum MyEnum { FirstValue, SecondValue }; + + Q_ENUM(MyEnum) + Q_PROPERTY(MyEnum enumProperty READ enumProperty) + + MyEnum enumProperty() const { return SecondValue; } +}; + +void tst_qqmlvaluetypes::enumProperties() +{ + QJSEngine engine; + + // When creating the property cache for the gadget when MyEnum is _not_ a registered + // meta-type, then QMetaProperty::type() will return QMetaType::Int and consequently + // property-read meta-calls will return an int (as expected in this test). However if we + // explicitly register the gadget, then QMetaProperty::type() will return the user-type + // and QQmlValueTypeWrapper should still handle that and return an integer/number for the + // enum property when it is read. + qRegisterMetaType<GadgetWithEnum::MyEnum>(); + + GadgetWithEnum g; + QJSValue value = engine.toScriptValue(g); + + QJSValue enumValue = value.property("enumProperty"); + QVERIFY(enumValue.isNumber()); + QCOMPARE(enumValue.toInt(), int(g.enumProperty())); +} + QTEST_MAIN(tst_qqmlvaluetypes) |