aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/doc/snippets/code/src_qml_qqmlengine.cpp
blob: 7057718b525d35ef4ad88a47e4c1c14ce32db22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 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]
}