diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2023-01-27 15:30:30 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2023-02-08 08:26:17 +0100 |
commit | fc8bb0ff5dcdb6891713057242039fde9d7bd2af (patch) | |
tree | a82f5ba8a8f3227b1d6d5d2d4179491e5f3b0411 /src/qml/doc/snippets | |
parent | f70844868a834fe988eb0716f6af4cbaacf89727 (diff) | |
download | qtdeclarative-fc8bb0ff5dcdb6891713057242039fde9d7bd2af.tar.gz |
Doc: Update QQmlContext documentation to discourage context properties
The snippets showing the use of setContextProperty() are intentionally
removed. You should not do such a thing.
Pick-to: 6.5
Fixes: QTBUG-106030
Change-Id: I1c5c217630aee8dd6e44f9f244b9ef2a8d2ef290
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/doc/snippets')
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] |