diff options
Diffstat (limited to 'src/qml/doc/snippets/qml/exposing-state')
4 files changed, 104 insertions, 0 deletions
diff --git a/src/qml/doc/snippets/qml/exposing-state/RequiredProperties.qml b/src/qml/doc/snippets/qml/exposing-state/RequiredProperties.qml new file mode 100644 index 0000000000..18425930de --- /dev/null +++ b/src/qml/doc/snippets/qml/exposing-state/RequiredProperties.qml @@ -0,0 +1,23 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +//![0] +pragma ComponentBehavior: Bound + +import QtQuick + +Window { + id: root + visible: true + + required property int thing + + Text { + anchors.fill: parent + text: "The thing is " + root.thing + } + + component Inner: QtObject { + objectName: "I can see " + root.thing + " because I'm bound." + } +} +//![0] diff --git a/src/qml/doc/snippets/qml/exposing-state/createWithInitialProperties.cpp b/src/qml/doc/snippets/qml/exposing-state/createWithInitialProperties.cpp new file mode 100644 index 0000000000..1e5f1859f9 --- /dev/null +++ b/src/qml/doc/snippets/qml/exposing-state/createWithInitialProperties.cpp @@ -0,0 +1,23 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QtQml/qqml.h> +#include <QtQml/qqmlengine.h> +#include <QtQml/qqmlcomponent.h> +#include <QtGui/qguiapplication.h> + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + +//![0] + QQmlEngine engine; + + QQmlComponent component(&engine, "MyModule", "RequiredProperties"); + QScopedPointer<QObject> o(component.createWithInitialProperties({ + {"thing", 11} + })); +//![0] + + return app.exec(); +} 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 diff --git a/src/qml/doc/snippets/qml/exposing-state/useSingleton.qml b/src/qml/doc/snippets/qml/exposing-state/useSingleton.qml new file mode 100644 index 0000000000..a9021a9241 --- /dev/null +++ b/src/qml/doc/snippets/qml/exposing-state/useSingleton.qml @@ -0,0 +1,9 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +//![0] +import QtQml + +QtObject { + objectName: "The thing is " + Singleton.thing +} +//![0] |