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
context-properties.qdoc
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qmllint-warnings-and-errors-context-properties.html
6\ingroup qmllint-warnings-and-errors
7
8\title Context properties
9\brief [context-properties] A context property was used.
10
11\qmllintwarningcategory context-properties
12
13\section1 Potential context property access detected
14
15\section2 What happened?
16A potential \l{Embedding C++ Objects into QML with Context Properties}{context property}
17 access was detected.
18
19\section2 Why is this bad?
20By using context properties in your QML code, you create a dependency from your
21QML code to the specific context you have in mind when writing it. This limits
22reusability of your code since the context may be different in other places
23where it might be used. Furthermore, the dependency is not declared. You never
24import the context or otherwise state what you expect. Therefore, anyone
25trying to reuse your code will have difficulties finding out whether the
26place where it is reused has a context sufficient for your code.
27
28QML tooling can't use the context property: the \l{Qt Quick Compiler}{compiler} can't
29compile this file to C++ and \l{qmllint} as well as \l{\QMLLS} can't provide useful
30warnings on that file.
31
32\section2 Example
33
34\code
35// main.cpp
36int main(int argc, char **argv)
37{
38 QGuiApplication app(argc, argv);
39 QQuickView view;
40 view.rootContext()
41 ->setContextProperty("myProperty", QDateTime::currentDateTime());
42 view.loadFromModule("MyModule", "Main");
43 view.show();
44 app.exec();
45}
46\endcode
47
48\qml
49// Main.qml
50import QtQuick
51
52Item {
53 Component.onCompleted: console.log(myProperty)
54}
55\endqml
56
57To fix this warning, you can:
58\list
59\li Make the property required.
60\li Use a singleton.
61\li Add an entry to \c{.contextProperties.ini}.
62\endlist
63
64\section3 Using a required property
65
66Use required properties for context properties that have different values in different
67components. To fix this warning with a required property, use one of the
68\c{setInitialProperties} methods instead of \l{QQmlContext::}{setContextProperty}:
69
70\list
71 \li \l{QQuickView::setInitialProperties},
72 \li \l{QQmlComponent::setInitialProperties},
73 \li \l{QQmlApplicationEngine::setInitialProperties},
74 \li \l{QQmlIncubator::setInitialProperties}, or
75 \li \l{QQuickWidget::setInitialProperties}.
76\endlist
77
78Also define the required property in \c{Main.qml}.
79
80\code
81// main.cpp
82int main(int argc, char **argv)
83{
84 QGuiApplication app(argc, argv);
85 QQuickView view;
86 view.setInitialProperties( { {"myProperty", QDateTime::currentDateTime()} } );
87 view.loadFromModule("MyModule", "Main");
88 view.show();
89 app.exec();
90}
91\endcode
92
93\qml
94// Main.qml
95import QtQuick
96
97Item {
98 required property date myProperty
99 Component.onCompleted: console.log(myProperty)
100}
101\endqml
102
103See also \l{Exposing State from C++ to QML}.
104
105\section3 Using a singleton
106
107Use singletons for context properties that have the same value in all components.
108To fix this warning with a \l{Singletons in QML}{singleton}, remove the
109\l{QQmlContext::}{setContextProperty} call, define a
110singleton and replace the context property usage with the singleton property.
111
112\code
113// mysingleton.h
114class MySingleton : public QObject {
115 Q_OBJECT
116 QML_SINGLETON
117 QML_ELEMENT
118 Q_PROPERTY(QDateTime myProperty MEMBER m_myProperty NOTIFY myPropertyChanged FINAL)
119
120 QDateTime m_myProperty = QDateTime::currentDateTime();
121signals:
122 void myPropertyChanged();
123};
124// main.cpp
125int main(int argc, char **argv)
126{
127 QGuiApplication app(argc, argv);
128 QQuickView view;
129 view.loadFromModule("MyModule", "Main");
130 view.show();
131 app.exec();
132}
133\endcode
134
135\qml
136// Main.qml
137import QtQuick
138
139Item {
140 Component.onCompleted: console.log(MySingleton.myProperty)
141}
142\endqml
143
144See also \l{Exposing State from C++ to QML}.
145
146\section3 Adding an entry to .contextProperties.ini
147
148If you can't replace the context property, create a
149\l{Context property settings}{.contextProperties.ini} file in your project
150source directory if there is none, and write the following content:
151
152\badcode
153[General]
154disableUnqualifiedAccess = "myProperty"
155warnOnUsage = "myProperty"
156disableHeuristic = false
157\endcode
158
159To silence this warning if the \c{.contextProperties.ini} file already exists,
160append the context property name to the already existing lists:
161
162\badcode
163[General]
164disableUnqualifiedAccess = "someOtherProperty,myProperty"
165warnOnUsage = "someOtherProperty,myProperty"
166disableHeuristic = false
167\endcode
168
169See also \l{Context property settings} for more information on the
170\c {.contextProperties.ini} format.
171
172*/