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