Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
singleton.h
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#ifndef SINGLETON_H
5#define SINGLETON_H
6
7#include <QtQml/qobject.h>
8#include <QtQml/qqml.h>
9#include <QtQml/qqmlengine.h>
10
11//![0]
12// Singleton.h
13class Singleton : public QObject
14{
15 Q_OBJECT
16 Q_PROPERTY(int thing READ thing WRITE setThing NOTIFY thingChanged FINAL)
19
20public:
22
23 int thing() const { return m_value; }
24 void setThing(int v)
25 {
26 if (v != m_value) {
27 m_value = v;
29 }
30 }
31
34
35private:
36 int m_value = 12;
37};
38//![0]
39
40inline void setTheThing(QQmlEngine *engine)
41{
42//![1]
43 Singleton *singleton
44 = engine->singletonInstance<Singleton *>("MyModule", "Singleton");
45 singleton->setThing(77);
46//![1]
47}
48
49#endif
Singleton(QObject *parent=nullptr)
Definition singleton.h:21
void setTheThing(QQmlEngine *engine)
[0]
Definition singleton.h:40