Skip to content

Commit

Permalink
Doc: add C++11 lambda examples for qmlRegisterSingletonType()
Browse files Browse the repository at this point in the history
Change-Id: I444137fd10041781df232447b8e2bf712582f079
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
  • Loading branch information
J-P Nurmi authored and ec1oud committed Feb 2, 2018
1 parent 23ea4cf commit 6958308
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/qml/doc/src/qmlfunctions.qdoc
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6958308

Please sign in to comment.