diff options
author | Pierre-Yves Siret <gr3cko@gmail.com> | 2019-12-20 01:51:39 +0100 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2020-01-15 14:01:08 +0000 |
commit | 43e3c21a0bad9141b6c35cc4ee4a58b4d824462a (patch) | |
tree | 4d3c1cc3e0a84eb0fa6d53acba4019a849d19102 | |
parent | 5a4ffa0de0ecc666a514ef60f0149a76d25b040d (diff) |
Add Q_GADGET Wrapper for QQmlProperty
This enable exposing a QQmlProperty as a value type to QML code.
Only object and name are exposed as a properties to keep it simple.
We might want later to expose the property of a Binding or a Behavior
as a property for complex usecases. This permits exposing it as one
self contained value instead of a pair of unrelated object and name.
Task-number: QTBUG-70964
Task-number: QTBUG-73892
Change-Id: I3a46212446f43f3a7e301943cb49d3a48c377de3
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r-- | src/qml/qml/qqmlengine.cpp | 2 | ||||
-rw-r--r-- | src/qml/qml/qqmlvaluetype.cpp | 15 | ||||
-rw-r--r-- | src/qml/qml/qqmlvaluetype_p.h | 11 | ||||
-rw-r--r-- | tests/auto/qml/qqmlvaluetypes/data/qmlproperty_read.qml | 9 | ||||
-rw-r--r-- | tests/auto/qml/qqmlvaluetypes/testtypes.h | 6 | ||||
-rw-r--r-- | tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp | 15 |
6 files changed, 55 insertions, 3 deletions
diff --git a/src/qml/qml/qqmlengine.cpp b/src/qml/qml/qqmlengine.cpp index d7001dbbe3..73d3ba1863 100644 --- a/src/qml/qml/qqmlengine.cpp +++ b/src/qml/qml/qqmlengine.cpp @@ -101,8 +101,6 @@ # endif #endif // Q_OS_WIN -Q_DECLARE_METATYPE(QQmlProperty) - QT_BEGIN_NAMESPACE // Declared in qqml.h diff --git a/src/qml/qml/qqmlvaluetype.cpp b/src/qml/qml/qqmlvaluetype.cpp index a4737b3da9..90a10c8d9a 100644 --- a/src/qml/qml/qqmlvaluetype.cpp +++ b/src/qml/qml/qqmlvaluetype.cpp @@ -48,6 +48,8 @@ #endif #include <private/qmetatype_p.h> +Q_DECLARE_METATYPE(QQmlProperty) + QT_BEGIN_NAMESPACE namespace { @@ -143,7 +145,8 @@ const QMetaObject *QQmlValueTypeFactoryImpl::metaObjectForMetaType(int t) if (t == qMetaTypeId<QItemSelectionRange>()) return &QQmlItemSelectionRangeValueType::staticMetaObject; #endif - + if (t == qMetaTypeId<QQmlProperty>()) + return &QQmlPropertyValueType::staticMetaObject; if (const QMetaObject *mo = QQml_valueTypeProvider()->metaObjectForMetaType(t)) return mo; break; @@ -580,6 +583,16 @@ void QQmlEasingValueType::setBezierCurve(const QVariantList &customCurveVariant) v = newEasingCurve; } +QObject *QQmlPropertyValueType::object() const +{ + return v.object(); +} + +QString QQmlPropertyValueType::name() const +{ + return v.name(); +} + QVariantList QQmlEasingValueType::bezierCurve() const { QVariantList rv; diff --git a/src/qml/qml/qqmlvaluetype_p.h b/src/qml/qml/qqmlvaluetype_p.h index 9c008859da..0d581c7be8 100644 --- a/src/qml/qml/qqmlvaluetype_p.h +++ b/src/qml/qml/qqmlvaluetype_p.h @@ -264,6 +264,17 @@ public: QVariantList bezierCurve() const; }; +struct QQmlPropertyValueType +{ + QQmlProperty v; + Q_PROPERTY(QObject *object READ object CONSTANT FINAL) + Q_PROPERTY(QString name READ name CONSTANT FINAL) + Q_GADGET +public: + QObject *object() const; + QString name() const; +}; + template<typename T> int qmlRegisterValueTypeEnums(const char *uri, int versionMajor, int versionMinor, const char *qmlName) { diff --git a/tests/auto/qml/qqmlvaluetypes/data/qmlproperty_read.qml b/tests/auto/qml/qqmlvaluetypes/data/qmlproperty_read.qml new file mode 100644 index 0000000000..b9c9ee779b --- /dev/null +++ b/tests/auto/qml/qqmlvaluetypes/data/qmlproperty_read.qml @@ -0,0 +1,9 @@ +import Test 1.0 +import QtQml 2.0 + +MyTypeObject { + property QtObject colorPropertyObject: colorProperty.object + property string colorPropertyName: colorProperty.name + property QtObject invalidPropertyObject: invalidProperty.object + property string invalidPropertyName: invalidProperty.name +} diff --git a/tests/auto/qml/qqmlvaluetypes/testtypes.h b/tests/auto/qml/qqmlvaluetypes/testtypes.h index 798c96e188..e11d831236 100644 --- a/tests/auto/qml/qqmlvaluetypes/testtypes.h +++ b/tests/auto/qml/qqmlvaluetypes/testtypes.h @@ -71,6 +71,8 @@ class MyTypeObject : public QObject Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY changed) Q_PROPERTY(QColor invalidColor READ invalidColor CONSTANT) Q_PROPERTY(QVariant variant READ variant NOTIFY changed) + Q_PROPERTY(QQmlProperty colorProperty READ colorProperty CONSTANT) + Q_PROPERTY(QQmlProperty invalidProperty READ invalidProperty CONSTANT) public: MyTypeObject() : @@ -173,6 +175,10 @@ public: QVariant variant() const { return sizef(); } + QQmlProperty colorProperty() { return QQmlProperty(this, "color"); } + + QQmlProperty invalidProperty() const { return QQmlProperty(); } + void emitRunScript() { emit runScript(); } signals: diff --git a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp index 3e9047cc5a..7c75743311 100644 --- a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp +++ b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp @@ -67,6 +67,7 @@ private slots: void color(); void variant(); void locale(); + void qmlproperty(); void bindingAssignment(); void bindingRead(); @@ -360,6 +361,20 @@ void tst_qqmlvaluetypes::locale() } } +void tst_qqmlvaluetypes::qmlproperty() +{ + QQmlComponent component(&engine, testFileUrl("qmlproperty_read.qml")); + MyTypeObject *object = qobject_cast<MyTypeObject *>(component.create()); + QVERIFY(object != nullptr); + + QCOMPARE(object->property("colorPropertyObject").value<QObject *>(), object); + QCOMPARE(object->property("colorPropertyName").toString(), "color"); + QCOMPARE(object->property("invalidPropertyObject").value<QObject *>(), nullptr); + QCOMPARE(object->property("invalidPropertyName").toString(), ""); + + delete object; +} + void tst_qqmlvaluetypes::sizereadonly() { { |