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