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
exposecppstate.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2023 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
/*!
4
\page qtqml-cppintegration-exposecppstate.html
5
\title Exposing State from C++ to QML
6
\brief Description of how to expose global state from C++ to QML
7
8
It is often desirable to expose some properties from C++ to all QML elements in a
9
particular component, all QML elements in a module, or even all QML elements
10
overall. You can do this by introducing singletons or by adding properties to the
11
root objects of select components.
12
13
\section1 Using Singletons
14
15
If you want to expose a number of global properties to all elements in a module
16
or all elements overall, you can define a singleton in C++. To do this, add the
17
\l{QML_ELEMENT} or \l{QML_NAMED_ELEMENT} macros and the \l{QML_SINGLETON} macro
18
to a class containing the properties you want to expose as \l{Q_PROPERTY}
19
declarations:
20
21
\snippet qml/exposing-state/singleton.h 0
22
23
Now you can access the \e thing property of the singleton from any QML code that
24
imports this module:
25
26
\snippet qml/exposing-state/useSingleton.qml 0
27
28
If you have placed your QML files in the same directory as the module (which
29
is highly recommended), the singleton is available from the implicit import
30
within your module. You don't need to import anything explicitly. If not, or if
31
you want to access the \e thing property from other modules, you do need to
32
import the module the singleton belongs to.
33
34
In order to set the value of the property from C++, you may need to retrieve the
35
singleton instance. For this purpose you may use
36
\l{QQmlEngine::singletonInstance}. The preferred way to do this is by giving a
37
module and type name as parameters:
38
39
\snippet qml/exposing-state/singleton.h 1
40
41
\section1 Using Object Properties
42
43
If you want to expose some properties to only the QML elements in a specific
44
component, you can add them as regular properties to the root object of the
45
component. In order to make sure they are actually set in all cases, you can
46
make them \l{Required Properties}. You might write your QML component as
47
follows:
48
49
\snippet qml/exposing-state/RequiredProperties.qml 0
50
51
We use an ID for the root element of the component and reference the property
52
by ID and name from any inner objects. In order to safely make the ID of
53
the root element available to any nested components, we use
54
\l{ComponentBehavior}.
55
56
Then, in C++, when you create an object from such a component, you need to make
57
sure to call the \l{QQmlComponent::createWithInitialProperties},
58
\l{QQmlApplicationEngine::setInitialProperties}, or
59
\l{QQuickView::setInitialProperties} in order to initialize the properties. For
60
example:
61
62
\snippet qml/exposing-state/createWithInitialProperties.cpp 0
63
64
This is assuming your module URI is \e MyModule and the module is available in
65
the QML import path.
66
*/
qtdeclarative
src
qml
doc
src
cppintegration
exposecppstate.qdoc
Generated on
for Qt by
1.14.0