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
qt6-changes.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page qml-changes-qt6.html
6 \title Changes to Qt QML
7 \ingroup changes-qt-5-to-6
8 \brief Migrate Qt QML to Qt 6.
9
10 Qt 6 is a result of the conscious effort to make the framework more
11 efficient and easy to use.
12
13 We try to maintain binary and source compatibility for all the public
14 APIs in each release. But some changes were inevitable in an effort to
15 make Qt a better framework.
16
17 In this topic we summarize those changes in Qt QML, and provide
18 guidance to handle them.
19
20 \section1 QML language
21
22 \section2 URL resolution
23
24 In Qt 5, relative urls were often, albeit inconsistently, directly resolved, especially when
25 assigned to an url property. In Qt 6 this is no longer the case.
26
27 If you had a QML file stored under "/home/qml/example.qml", and "example.qml" contained
28 \code
29 property url imageFolder: "./images"
30 \endcode
31 then the url property would store the URL "/home/qml/images". This made it impossible to use
32 relative URLs in QML in this way, so in Qt 6, the URL stays relative, and only gets resolved
33 when this is required (e.g. when it is used as the source of an Image component).
34 If you depend on the old behavior, you can use \c Qt.resolvedUrl
35
36 For example, if you have code like
37
38 \code
39 property url imageFolder: "./images"
40 \endcode
41
42 you can rewrite it as
43
44 \code
45 property url imageFolder: Qt.resolvedUrl("./images")
46 \endcode
47
48 Qt.resolvedUrl can be used in both Qt 5 and 6.
49
50 \section2 Variant Properties
51
52 \c variant properties, which have been marked as obsolete since Qt 5, are now treated in exactly
53 the same way as \c var properties.
54 Code that relied on implicit string conversion triggered on assignment to variant properties
55 should be updated to explicitly create an object of the correct type.
56
57 For example, if you have code like
58
59 \code
60 property variant myColor: "red"
61 \endcode
62
63 you can rewrite it as
64
65 \code
66 property variant myColor: Qt.color("red")
67 \endcode
68
69 Implicit conversions were done for strings that could be parsed as
70 \list
71 \li color (use Qt.color instead),
72 \li date (use the Date object instead),
73 \li rect (use Qt.rect instead) and
74 \li size (use Qt.size instead)
75 \endlist
76
77 \c variant still remains a deprecated keyword in Qt 6, though new code is strongly encouraged to
78 use \c var properties instead.
79
80 \note If the type of the property is known not to change, use a property of the concrete type,
81 instead of a \c var property.
82
83 \note These conversions were also applied to \c QVariant properties of classes registered with
84 the engine. As with \c variant properties, code that relied on implicit string conversions need
85 to use the corresponding functions of the Qt object.
86
87 \section1 Source Incompatible API Changes
88
89 \section2 Changed API
90
91 \c QQmlListProperty's \c CountFunction and \c AtFunction have been changed to use \c qsizetype
92 instead of \c int to align with the corresponding changes in Qt's containers.
93
94 For example, if you have code like
95
96 \code
97 int myCountFunction(QQmlListProperty<MyType> *);
98 MyType *myAtFunction(QQmlListProperty<MyType> *, int);
99
100 QQmlListProperty<MyType> myReadOnlyList(containingObject, container, &myCountFunction,
101 &myAtFunction);
102 \endcode
103
104 you can rewrite it as
105
106 \code
107 qsizetype myCountFunction(QQmlListProperty<MyType> *);
108 MyType *myAtFunction(QQmlListProperty<MyType> *, qsizetype);
109
110 QQmlListProperty<MyType> myReadOnlyList(containingObject, container, &myCountFunction,
111 &myAtFunction);
112 \endcode
113
114 Code which needs to supports both Qt 5 and Qt 6 can either use a typedef which is \c int in Qt 5
115 and \c qsizetype in Qt 6, or use \c QList::size_type, which already is such a type alias.
116
117 \section2 Removed API
118
119 Various deprecated functions have been removed.
120
121 \list
122 \li The QQmlListProperty constructor taking a reference has been removed.
123
124 For example, if you have code like
125
126 \code
127 QQmlListProperty<QObject>(owner, owner->objectList);
128 \endcode
129
130 you can rewrite it as
131
132 \code
133 QQmlListProperty<QObject>(owner, &owner->objectList);
134 \endcode
135
136 \li The functions \c qmlDebug, \c qmlInfo, \c qmlWarning, \c qmlContext and \c qmlEngine used
137 to exist both in the global namespace (or Qt namespace in namespaced builds), and in the \c
138 QtQml namespace. These functions now exist only in the global namespace.
139
140 For example, if you have code like
141
142 \code
143 QQmlEngine *engine = QtQml::qmlEngine(qmlObject);
144 \endcode
145
146 you can rewrite it as
147
148 \code
149 QQmlEngine *engine = qmlEngine(qmlObject);
150 \endcode
151
152 \li The \c qmlRegisterType overload taking no arguments has been removed. Use
153 \c qmlRegisterAnonymousType instead, or switch to declarative type registration with
154 \c QML_ANONYMOUS.
155
156 For example, if you have code like
157
158 \code
159 class AnonymousType : public QObject {
160 // ...
161 };
162
163 qmlRegisterType<AnonymousType>();
164 \endcode
165
166 you can rewrite it as
167
168 \code
169 class AnonymousType : public QObject {
170 // ...
171 };
172
173 qmlRegisterAnonymousType<AnonymousType>("MyModule", 1);
174 \endcode
175
176 Or alternatively
177 \code
178 class AnonymousType : public QObject {
179 QML_ANONYMOUS
180 // ...
181 };
182 \endcode
183
184 \li The overloads of \c qmlRegisterExtendedType and \c qmlRegisterInterface
185 which take no version argument have been removed. Use the overloads providing a
186 version, or switch to declarative type registration with QML_EXTENDED
187 and QML_INTERFACE.
188
189 For example, if you have code like
190
191 \code
192 struct GreetInterface
193 {
194 virtual ~GreetInterface();
195 virtual void greet();
196 };
197 Q_DECLARE_INTERFACE(GreetInterface, "org.hi.GreetInterface")
198
199 qmlRegisterInterface<GreetInterface>("Greeter");
200 \endcode
201
202 you can rewrite it as
203
204 \code
205 struct GreetInterface
206 {
207 virtual ~GreetInterface();
208 virtual void greet();
209 };
210 Q_DECLARE_INTERFACE(GreetInterface, "org.hi.GreetInterface")
211
212 qmlRegisterInterface<GreetInterface>("Greeter", 1);
213 \endcode
214
215 Alternatively
216
217 \code
218 struct GreetInterface
219 {
220 QML_INTERFACE(Greeter)
221 virtual ~GreetInterface();
222 virtual void greet();
223 };
224 Q_DECLARE_INTERFACE(GreetInterface, "org.hi.GreetInterface")
225 \endcode
226
227 \note In new code, declarative type registration should be preferred.
228
229 \li The function \c QJSValue::engine has been removed. If access to the engine is required, a
230 reference to it must be stored instead.
231
232 \li \c qmlAttachedPropertiesObjectById and \c qmlAttachedPropertiesObject(int *, const QObject *,
233 const QMetaObject *, bool) have been removed. Use the
234 \c qmlAttachedPropertiesObject(QObject *, QQmlAttachedPropertiesFunc, bool) overload of
235 \c qmlAttachedPropertiesObject instead.
236
237 \li \c QJSEngine::installTranslatorFunctions has been removed. \c QJSEngine::installExtensions
238 is available as a replacement.
239
240 For example, if you have code like
241
242 \code
243 QJSEngine engine;
244 engine.installTranslatorFunctions();
245 \endcode
246
247 you can rewrite it as
248
249 \code
250 QJSEngine engine;
251 engine.installExtensions(QJSEngine::TranslationExtension);
252 \endcode
253
254 \endlist
255
256
257*/