diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2018-01-29 16:58:22 +0100 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2018-02-02 07:15:40 +0000 |
commit | 6958308c09ceda855a30c5a2d491f078c5104071 (patch) | |
tree | 04495ef2747a87c7edf04eb92cefc3132ec2016f | |
parent | 23ea4cf5a3babd73a4d398410e2a21685245e0a0 (diff) |
Doc: add C++11 lambda examples for qmlRegisterSingletonType()
Change-Id: I444137fd10041781df232447b8e2bf712582f079
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
-rw-r--r-- | src/qml/doc/src/qmlfunctions.qdoc | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/qml/doc/src/qmlfunctions.qdoc b/src/qml/doc/src/qmlfunctions.qdoc index 8a62a18eec..f939ddbcf3 100644 --- a/src/qml/doc/src/qmlfunctions.qdoc +++ b/src/qml/doc/src/qmlfunctions.qdoc @@ -324,6 +324,19 @@ qmlRegisterSingletonType("Qt.example.qjsvalueApi", 1, 0, "MyApi", example_qjsvalue_singletontype_provider); \endcode + Alternatively, you can use a C++11 lambda: + + \code + qmlRegisterSingletonType("Qt.example.qjsvalueApi", 1, 0, "MyApi", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QJSValue { + Q_UNUSED(engine) + + static int seedValue = 5; + QJSValue example = scriptEngine->newObject(); + example.setProperty("someProperty", seedValue++); + return example; + }); + \endcode + In order to use the registered singleton type in QML, you must import the singleton type. \qml import QtQuick 2.0 @@ -423,6 +436,18 @@ qmlRegisterSingletonType<SingletonTypeExample>("Qt.example.qobjectSingleton", 1, 0, "MyApi", example_qobject_singletontype_provider); \endcode + Alternatively, you can use a C++11 lambda: + + \code + qmlRegisterSingletonType<SingletonTypeExample>("Qt.example.qjsvalueApi", 1, 0, "MyApi", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * { + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + + SingletonTypeExample *example = new SingletonTypeExample(); + return example; + }); + \endcode + In order to use the registered singleton type in QML, you must import the singleton type. \qml import QtQuick 2.0 |