Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
8It is often desirable to expose some properties from C++ to all QML elements in a
9particular component, all QML elements in a module, or even all QML elements
10overall. You can do this by introducing singletons or by adding properties to the
11root objects of select components.
12
13\section1 Using Singletons
14
15If you want to expose a number of global properties to all elements in a module
16or 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
18to a class containing the properties you want to expose as \l{Q_PROPERTY}
19declarations:
20
21\snippet qml/exposing-state/singleton.h 0
22
23Now you can access the \e thing property of the singleton from any QML code that
24imports this module:
25
26\snippet qml/exposing-state/useSingleton.qml 0
27
28If you have placed your QML files in the same directory as the module (which
29is highly recommended), the singleton is available from the implicit import
30within your module. You don't need to import anything explicitly. If not, or if
31you want to access the \e thing property from other modules, you do need to
32import the module the singleton belongs to.
33
34In order to set the value of the property from C++, you may need to retrieve the
35singleton instance. For this purpose you may use
36\l{QQmlEngine::singletonInstance}. The preferred way to do this is by giving a
37module and type name as parameters:
38
39\snippet qml/exposing-state/singleton.h 1
40
41\section1 Using Object Properties
42
43If you want to expose some properties to only the QML elements in a specific
44component, you can add them as regular properties to the root object of the
45component. In order to make sure they are actually set in all cases, you can
46make them \l{Required Properties}. You might write your QML component as
47follows:
48
49\snippet qml/exposing-state/RequiredProperties.qml 0
50
51We use an ID for the root element of the component and reference the property
52by ID and name from any inner objects. In order to safely make the ID of
53the root element available to any nested components, we use
54\l{ComponentBehavior}.
55
56Then, in C++, when you create an object from such a component, you need to make
57sure to call the \l{QQmlComponent::createWithInitialProperties},
58\l{QQmlApplicationEngine::setInitialProperties}, or
59\l{QQuickView::setInitialProperties} in order to initialize the properties. For
60example:
61
62\snippet qml/exposing-state/createWithInitialProperties.cpp 0
63
64This is assuming your module URI is \e MyModule and the module is available in
65the QML import path.
66*/