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
syntax.qdoc
Go to the documentation of this file.
1// Copyright (C) 2023 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-syntax.html
6\ingroup qmllint-warnings-and-errors
7
8\title Syntax
9\brief [syntax] Various syntactic errors.
10
11\qmllintwarningcategory syntax
12
13\section1 Nested inline components are not supported
14
15\section2 What happened?
16You defined an \l{qtqml-documents-definetypes.html#inline-components}{inline component}
17inside another inline component.
18
19\section2 Why is this bad?
20The QML language does not allow nested inline components. Always define inline components
21inside the root item of the QML file.
22
23\section2 Example
24\qml
25import QtQuick
26
27Item {
28 component Correct: Item {
29 component Evil: Item { ... }
30 ...
31 }
32}
33
34\endqml
35To fix this warning, move all components to the root item of the QML file.
36\qml
37import QtQuick
38
39Item {
40 component NotEvilAnymore: Item { ... }
41 component Correct: Item {
42 ...
43 }
44}
45
46\endqml
47
48\section1 Inline component declaration must be followed by a typename
49
50\section2 What happened?
51You defined an \l{qtqml-documents-definetypes.html#inline-components}{inline component}
52with an invalid base type.
53
54\section2 Why is this bad?
55Inline components need a base type to inherit from.
56
57\section2 Example
58\qml
59import QtQuick
60
61Item {
62 property Item someProperty
63 component InlineComponent: someProperty {}
64}
65
66\endqml
67In this case, \c someProperty is not a valid type name, as it is a property name.
68To fix this warning, use a valid Type as the component's base type:
69\qml
70import QtQuick
71
72Item {
73 property Item someProperty
74 component InlineComponent: Item { ... }
75}
76
77\endqml
78
79\section1 Invalid alias expression: an initializer is needed
80
81\section2 What happened?
82You defined a \l{qtqml-syntax-objectattributes.html#property-aliases}{property alias}
83without its aliased property.
84
85\section2 Why is this bad?
86Alias properties always need to have their aliased property or id in their definition.
87
88\section2 Example
89\qml
90import QtQuick
91
92Item {
93 id: root
94 property int someProperty
95 property alias aliasProperty
96}
97
98\endqml
99To fix this warning, replace the alias with a normal property, or add the missing
100aliased property:
101\qml
102import QtQuick
103
104Item {
105 id: root
106 property int someProperty
107 property alias withAliasedProperty: root.someProperty
108}
109
110\endqml
111
112\section1 Invalid alias expression: only ids and field member expressions can be aliased
113
114\section2 What happened?
115You defined a \l{qtqml-syntax-objectattributes.html#property-aliases}{property alias}
116that aliases an expression other than an ID or a field member expression.
117
118A field member expression is an expression of the form \c {someId.someProperty}.
119
120\section2 Why is this bad?
121Alias properties always need to have their aliased property in their definition, and can't
122bind to other expressions than IDs and field member expressions.
123
124\section2 Example
125\qml
126import QtQuick
127
128Item {
129 property int p
130 property alias someProperty: p + 1
131}
132
133\endqml
134To fix this warning, replace the alias with a normal property or bind it to an id or
135field member expression:
136\qml
137import QtQuick
138
139Item {
140 id: root
141 property int p
142 property int someProperty: p + 1
143 property alias alternative: root.p
144}
145
146\endqml
147
148\section1 Id must be followed by an identifier
149
150\section2 What happened?
151You defined an \l{qtqml-syntax-objectattributes.html#the-id-attribute}{id} without
152a value.
153
154\section2 Why is this bad?
155The QML language does not allow empty ids.
156
157\section2 Example
158\qml
159import QtQuick
160
161Item {
162 id:;
163}
164
165\endqml
166To fix this warning, bind the id to a valid name:
167\qml
168import QtQuick
169
170Item {
171 id: root;
172}
173
174\endqml
175
176\section1 Failed to parse id
177
178\section2 What happened?
179You bound an \l{qtqml-syntax-objectattributes.html#the-id-attribute}{id} to an
180expression other than a name.
181
182\section2 Why is this bad?
183The QML language only allows names as bindings to ids; more complex expressions
184can't be used.
185
186\section2 Example
187\qml
188import QtQuick
189
190Item {
191 property int a
192 property int b
193 function f() {
194 if (true)
195 return a
196 return b
197 }
198
199 id: f()
200}
201\endqml
202To fix this warning, bind the id to a valid name or declare a property and set up a binding:
203\qml
204import QtQuick
205
206Item {
207 property int a
208 property int b
209 function f() {
210 if (true)
211 return a
212 return b
213 }
214
215 id: someItem // it would be confusing to call it `f` like the function
216 property int alternative: f()
217}
218\endqml
219
220\section1 Declaring an object which is not a QML object as a list member
221
222\section2 What happened?
223You added an expression other than an \l{qtqml-typesystem-objecttypes.html}{object} into a
224list of objects.
225
226\section2 Why is this bad?
227The QML language only allows objects in object lists.
228
229\section2 Example
230\qml
231import QtQuick
232
233Item {
234 property int hello
235 property list<Item> myList: [
236 Item {}, hello{}
237 ]
238}
239
240\endqml
241To fix this warning, use a valid object type, or remove the item from the list:
242\qml
243import QtQuick
244
245Item {
246 component Hello: Item {}
247 property list<Item> myList: [
248 Item {}, Hello{}
249 ]
250}
251
252\endqml
253
254\section1 Enums declared inside of inline components are ignored
255
256\section2 What happened?
257You defined an \l{qtqml-syntax-objectattributes.html#enumeration-attributes}{enum}
258inside an inline component.
259
260\section2 Why is this bad?
261The QML language only allows enum definitions inside the root
262item of the QML file. Enums declared inside an inline
263component are unusable, even inside the inline component. The same applies to
264enums declared inside non-root QML objects.
265
266\section2 Example
267\qml
268import QtQuick
269
270Item {
271 component MyInlineComponent: Item {
272 enum MyEnum { Hello, World }
273 }
274}
275
276\endqml
277To fix this warning, move the enum declaration into the root element of the
278QML file:
279\qml
280import QtQuick
281
282Item {
283 enum MyEnum { Hello, World }
284 component MyInlineComponent: Item {
285 }
286}
287
288\endqml
289
290\section1 Unknown argument to pragma
291
292\section2 What happened?
293You specified an invalid argument to a \l{qtqml-documents-structure.html#pragmas}{pragma}.
294
295\section2 Why is this bad?
296The pragma will have no effect.
297
298\section2 Example
299\qml
300pragma ComponentBehavior: Buond
301import QtQuick
302
303Item {
304}
305
306\endqml
307You can fix this warning by removing the pragma or fixing a potential typo:
308\qml
309pragma ComponentBehavior: Bound
310import QtQuick
311
312Item {
313}
314
315\endqml
316*/