aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/doc/snippets/qml/exposing-state/singleton.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/qml/doc/snippets/qml/exposing-state/singleton.h')
-rw-r--r--src/qml/doc/snippets/qml/exposing-state/singleton.h49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/qml/doc/snippets/qml/exposing-state/singleton.h b/src/qml/doc/snippets/qml/exposing-state/singleton.h
new file mode 100644
index 0000000000..e600531883
--- /dev/null
+++ b/src/qml/doc/snippets/qml/exposing-state/singleton.h
@@ -0,0 +1,49 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#ifndef SINGLETON_H
+#define SINGLETON_H
+
+#include <QtQml/qobject.h>
+#include <QtQml/qqml.h>
+#include <QtQml/qqmlengine.h>
+
+//![0]
+// Singleton.h
+class Singleton : public QObject
+{
+ Q_OBJECT
+ Q_PROPERTY(int thing READ thing WRITE setThing NOTIFY thingChanged FINAL)
+ QML_ELEMENT
+ QML_SINGLETON
+
+public:
+ Singleton(QObject *parent = nullptr) : QObject(parent) {}
+
+ int thing() const { return m_value; }
+ void setThing(int v)
+ {
+ if (v != m_value) {
+ m_value = v;
+ emit thingChanged();
+ }
+ }
+
+signals:
+ void thingChanged();
+
+private:
+ int m_value = 12;
+};
+//![0]
+
+inline void setTheThing(QQmlEngine *engine)
+{
+//![1]
+ Singleton *singleton
+ = engine->singletonInstance<Singleton *>("MyModule", "Singleton");
+ singleton->setThing(77);
+//![1]
+}
+
+#endif