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?
16
A potential \l{Embedding C++ Objects into QML with Context Properties}{context property}
17
access was detected.
18
19
\section2 Why is this bad?
20
By using context properties in your QML code, you create a dependency from your
21
QML code to the specific context you have in mind when writing it. This limits
22
reusability of your code since the context may be different in other places
23
where it might be used. Furthermore, the dependency is not declared. You never
24
import the context or otherwise state what you expect. Therefore, anyone
25
trying to reuse your code will have difficulties finding out whether the
26
place where it is reused has a context sufficient for your code.
27
28
QML tooling can't use the context property: the \l{Qt Quick Compiler}{compiler} can't
29
compile this file to C++ and \l{qmllint} as well as \l{\QMLLS} can't provide useful
30
warnings on that file.
31
32
\section2 Example
33
34
\code
35
// main.cpp
36
int 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
50
import QtQuick
51
52
Item {
53
Component.onCompleted: console.log(myProperty)
54
}
55
\endqml
56
57
To 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
66
Use required properties for context properties that have different values in different
67
components. 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
78
Also define the required property in \c{Main.qml}.
79
80
\code
81
// main.cpp
82
int 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
95
import QtQuick
96
97
Item {
98
required property date myProperty
99
Component.onCompleted: console.log(myProperty)
100
}
101
\endqml
102
103
See also \l{Exposing State from C++ to QML}.
104
105
\section3 Using a singleton
106
107
Use singletons for context properties that have the same value in all components.
108
To fix this warning with a \l{Singletons in QML}{singleton}, remove the
109
\l{QQmlContext::}{setContextProperty} call, define a
110
singleton and replace the context property usage with the singleton property.
111
112
\code
113
// mysingleton.h
114
class 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();
121
signals:
122
void myPropertyChanged();
123
};
124
// main.cpp
125
int 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
137
import QtQuick
138
139
Item {
140
Component.onCompleted: console.log(MySingleton.myProperty)
141
}
142
\endqml
143
144
See also \l{Exposing State from C++ to QML}.
145
146
\section3 Adding an entry to .contextProperties.ini
147
148
If you can't replace the context property, create a
149
\l{Context property settings}{.contextProperties.ini} file in your project
150
source directory if there is none, and write the following content:
151
152
\badcode
153
[General]
154
disableUnqualifiedAccess = "myProperty"
155
warnOnUsage = "myProperty"
156
disableHeuristic = false
157
\endcode
158
159
To silence this warning if the \c{.contextProperties.ini} file already exists,
160
append the context property name to the already existing lists:
161
162
\badcode
163
[General]
164
disableUnqualifiedAccess = "someOtherProperty,myProperty"
165
warnOnUsage = "someOtherProperty,myProperty"
166
disableHeuristic = false
167
\endcode
168
169
See also \l{Context property settings} for more information on the
170
\c {.contextProperties.ini} format.
171
172
*/
qtdeclarative
src
qml
doc
src
qmllint
context-properties.qdoc
Generated on
for Qt by
1.16.1