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?
16
You defined an \l{qtqml-documents-definetypes.html#inline-components}{inline component}
17
inside another inline component.
18
19
\section2 Why is this bad?
20
The QML language does not allow nested inline components. Always define inline components
21
inside the root item of the QML file.
22
23
\section2 Example
24
\qml
25
import QtQuick
26
27
Item {
28
component Correct: Item {
29
component Evil: Item { ... }
30
...
31
}
32
}
33
34
\endqml
35
To fix this warning, move all components to the root item of the QML file.
36
\qml
37
import QtQuick
38
39
Item {
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?
51
You defined an \l{qtqml-documents-definetypes.html#inline-components}{inline component}
52
with an invalid base type.
53
54
\section2 Why is this bad?
55
Inline components need a base type to inherit from.
56
57
\section2 Example
58
\qml
59
import QtQuick
60
61
Item {
62
property Item someProperty
63
component InlineComponent: someProperty {}
64
}
65
66
\endqml
67
In this case, \c someProperty is not a valid type name, as it is a property name.
68
To fix this warning, use a valid Type as the component's base type:
69
\qml
70
import QtQuick
71
72
Item {
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?
82
You defined a \l{qtqml-syntax-objectattributes.html#property-aliases}{property alias}
83
without its aliased property.
84
85
\section2 Why is this bad?
86
Alias properties always need to have their aliased property or id in their definition.
87
88
\section2 Example
89
\qml
90
import QtQuick
91
92
Item {
93
id: root
94
property int someProperty
95
property alias aliasProperty
96
}
97
98
\endqml
99
To fix this warning, replace the alias with a normal property, or add the missing
100
aliased property:
101
\qml
102
import QtQuick
103
104
Item {
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?
115
You defined a \l{qtqml-syntax-objectattributes.html#property-aliases}{property alias}
116
that aliases an expression other than an ID or a field member expression.
117
118
A field member expression is an expression of the form \c {someId.someProperty}.
119
120
\section2 Why is this bad?
121
Alias properties always need to have their aliased property in their definition, and can't
122
bind to other expressions than IDs and field member expressions.
123
124
\section2 Example
125
\qml
126
import QtQuick
127
128
Item {
129
property int p
130
property alias someProperty: p + 1
131
}
132
133
\endqml
134
To fix this warning, replace the alias with a normal property or bind it to an id or
135
field member expression:
136
\qml
137
import QtQuick
138
139
Item {
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?
151
You defined an \l{qtqml-syntax-objectattributes.html#the-id-attribute}{id} without
152
a value.
153
154
\section2 Why is this bad?
155
The QML language does not allow empty ids.
156
157
\section2 Example
158
\qml
159
import QtQuick
160
161
Item {
162
id:;
163
}
164
165
\endqml
166
To fix this warning, bind the id to a valid name:
167
\qml
168
import QtQuick
169
170
Item {
171
id: root;
172
}
173
174
\endqml
175
176
\section1 Failed to parse id
177
178
\section2 What happened?
179
You bound an \l{qtqml-syntax-objectattributes.html#the-id-attribute}{id} to an
180
expression other than a name.
181
182
\section2 Why is this bad?
183
The QML language only allows names as bindings to ids; more complex expressions
184
can't be used.
185
186
\section2 Example
187
\qml
188
import QtQuick
189
190
Item {
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
202
To fix this warning, bind the id to a valid name or declare a property and set up a binding:
203
\qml
204
import QtQuick
205
206
Item {
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?
223
You added an expression other than an \l{qtqml-typesystem-objecttypes.html}{object} into a
224
list of objects.
225
226
\section2 Why is this bad?
227
The QML language only allows objects in object lists.
228
229
\section2 Example
230
\qml
231
import QtQuick
232
233
Item {
234
property int hello
235
property list<Item> myList: [
236
Item {}, hello{}
237
]
238
}
239
240
\endqml
241
To fix this warning, use a valid object type, or remove the item from the list:
242
\qml
243
import QtQuick
244
245
Item {
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?
257
You defined an \l{qtqml-syntax-objectattributes.html#enumeration-attributes}{enum}
258
inside an inline component.
259
260
\section2 Why is this bad?
261
The QML language only allows enum definitions inside the root
262
item of the QML file. Enums declared inside an inline
263
component are unusable, even inside the inline component. The same applies to
264
enums declared inside non-root QML objects.
265
266
\section2 Example
267
\qml
268
import QtQuick
269
270
Item {
271
component MyInlineComponent: Item {
272
enum MyEnum { Hello, World }
273
}
274
}
275
276
\endqml
277
To fix this warning, move the enum declaration into the root element of the
278
QML file:
279
\qml
280
import QtQuick
281
282
Item {
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?
293
You specified an invalid argument to a \l{qtqml-documents-structure.html#pragmas}{pragma}.
294
295
\section2 Why is this bad?
296
The pragma will have no effect.
297
298
\section2 Example
299
\qml
300
pragma ComponentBehavior: Buond
301
import QtQuick
302
303
Item {
304
}
305
306
\endqml
307
You can fix this warning by removing the pragma or fixing a potential typo:
308
\qml
309
pragma ComponentBehavior: Bound
310
import QtQuick
311
312
Item {
313
}
314
315
\endqml
316
*/
qtdeclarative
src
qml
doc
src
qmllint
syntax.qdoc
Generated on
for Qt by
1.14.0