aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/doc/snippets/code/src_qml_qqmlengine.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/qml/doc/snippets/code/src_qml_qqmlengine.cpp')
-rw-r--r--src/qml/doc/snippets/code/src_qml_qqmlengine.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/qml/doc/snippets/code/src_qml_qqmlengine.cpp b/src/qml/doc/snippets/code/src_qml_qqmlengine.cpp
new file mode 100644
index 0000000000..ff4cf634a0
--- /dev/null
+++ b/src/qml/doc/snippets/code/src_qml_qqmlengine.cpp
@@ -0,0 +1,51 @@
+// Copyright (C) 2020 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+//! [0]
+class MySingleton : public QObject {
+ Q_OBJECT
+
+ // Register as default constructed singleton.
+ QML_ELEMENT
+ QML_SINGLETON
+
+ static int typeId;
+ // ...
+};
+//! [0]
+
+/*
+//! [1]
+ MySingleton::typeId = qmlTypeId(...);
+//! [1]
+*/
+
+void wrapper2() {
+//! [2]
+ // Retrieve as QObject*
+ QQmlEngine engine;
+ MySingleton* instance = engine.singletonInstance<MySingleton*>(MySingleton::typeId);
+//! [2]
+}
+
+/*
+//! [3]
+ // Register with QJSValue callback
+ int typeId = qmlRegisterSingletonType(...);
+//! [3]
+*/
+
+void wrapper4(int typeId) {
+//! [4]
+ // Retrieve as QJSValue
+ QQmlEngine engine;
+ QJSValue instance = engine.singletonInstance<QJSValue>(typeId);
+//! [4]
+}
+
+void wrapper5() {
+///! [5]
+ QQmlEngine engine;
+ MySingleton *singleton = engine.singletonInstance<MySingleton *>("mymodule", "MySingleton");
+///! [5]
+}