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