diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2023-10-11 13:33:37 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-10-18 06:37:32 +0000 |
commit | 4cc830764553fc78b1e848a62d066caed973fae4 (patch) | |
tree | ef884b1736fafdb5ca00ee983101b19c72f450ce | |
parent | 5e8483841844e92c0b28e9490bc8f6dea81c9b4a (diff) |
Amend code snippets for QML_ELEMENT to show how to expose enums
You can expose enums from either object types or namespaces. The note
about namespaces had no snippet at all and the one about object types
didn't show what was actually exposed to QML.
Also fix the code snippet for qmlRegisterUncreatableMetaObject().
Fixes: QTBUG-113188
Change-Id: I94f8d8f21cb6690689de0cb98f4426daff6e72a2
Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 377501f35bf5014da405a3fa5c36d420331d05f9)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit c05eb170d5b2c20257d922ba237cd2d35ebd2b57)
-rw-r--r-- | src/qml/doc/src/qmlfunctions.qdoc | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/src/qml/doc/src/qmlfunctions.qdoc b/src/qml/doc/src/qmlfunctions.qdoc index 216d7e9975..6b6b2a2313 100644 --- a/src/qml/doc/src/qmlfunctions.qdoc +++ b/src/qml/doc/src/qmlfunctions.qdoc @@ -9,14 +9,24 @@ class or namespace name as the QML element name. For example, this makes the C++ class \c Slider available as a QML type - named \c Slider. + named \c Slider. All its properties, invokable methods and enums are exposed. \code class Slider : public QObject { Q_OBJECT QML_ELEMENT - ... + Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged FINAL) + // ... + public: + enum Slippiness { + Dry, Wet, Icy + }; + Q_ENUM(Slippiness) + + Q_INVOKABLE void slide(Slippiness slippiness); + + // ... } \endcode @@ -46,12 +56,34 @@ import com.mycompany.qmlcomponents 1.0 Slider { + value: 12 + Component.onCompleted: slide(Slider.Icy) + // ... } \endqml You can also make namespaces tagged with Q_NAMESPACE available this way, in - order to expose any enums tagged with Q_ENUM_NS they contain. + order to expose any enums tagged with Q_ENUM_NS they contain: + + \code + namespace MyNamespace { + Q_NAMESPACE + QML_ELEMENT + + enum MyEnum { + Key1, + Key2, + }; + Q_ENUM_NS(MyEnum) + } + \endcode + + In QML, you can then use the enums: + + \qml + Component.onCompleted: console.log(MyNamespace.Key2) + \endqml \b{NOTE:} When classes have the same name but are located in different namespaces using \l QML_ELEMENT on both of them will cause a conflict. @@ -834,7 +866,7 @@ Key1, Key2, }; - Q_ENUM(MyEnum) + Q_ENUM_NS(MyEnum) } //... |