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
valuetypes.qdoc
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3/*!
4\page qtqml-typesystem-valuetypes.html
5\title QML Value Types
6\brief Description of QML value types
7
8QML supports built-in and custom value types.
9
10A \e{value type} is one that is conceptually passed by value rather than by
11reference, such as an \c int or a \c string. This contrasts with
12\l{qtqml-typesystem-topic.html#qml-object-types}{QML Object Types}. Object types
13are passed by reference. If you assign an instance of an object type to two
14different properties, both properties carry the same value. Modifying the object
15is reflected in both properties. If you assign an instance of a value type to
16two different properties, the properties carry separate values. If you modify
17one of them, the other one stays the same. Value types are only conceptually
18passed by value since it must still be possible to interact with them as if they
19were JavaScript objects. To facilitate this, in reality they are passed as
20\l{QML Value Type and Sequence References}{Value Type References} when you access
21them from JavaScript code.
22
23Unlike an object type, a value type cannot be used to declare QML objects:
24it is not possible, for example, to declare an \c int{} object or a \c size{} object.
25
26Value types can be used to refer to:
27
28\list
29\li A single value (e.g. \l int refers to a single number)
30\li A value that contains properties and methods (e.g. \l size refers to a value with \c width and \c height properties)
31\li The generic type \l{var}. It can hold values of any other type but is itself a value type.
32\endlist
33
34When a variable or property holds a value type and it is assigned to another
35variable or property, then a copy of the value is made.
36
37\sa {qtqml-typesystem-topic.html}{The QML Type System}
38
39
40\section1 Available Value Types
41
42Some value types are supported by the engine by default and do not require an
43\l {Import Statements}{import statement} to be used, while others do require
44the client to import the module which provides them.
45All of the value types listed below may be used as a \c property type in a QML
46document, with the following exceptions:
47\list
48 \li \c void, which marks the absence of a value
49 \li \c list must be used in conjunction with an object or value type as element
50\endlist
51
52\section2 Built-in Value Types Provided By The QML Language
53
54The built-in value types supported natively in the \l{The QML Reference}{QML language} are listed below:
55\annotatedlist qmlvaluetypes
56
57\section2 Value Types Provided By QML Modules
58
59QML modules may extend the QML language with more value types.
60
61For instance, the value types provided by the \c QtQml module are:
62\annotatedlist qtqmlvaluetypes
63
64The value types provided by the \c QtQuick module are:
65\annotatedlist qtquickvaluetypes
66
67The \l{QtQml::Qt}{Qt} global object provides \l{globalqtobjecttypes}{useful functions} for manipulating values of value
68types for the \l{Qt Qml} and \l{Qt Quick} modules.
69
70Other Qt modules will document their value types on their respective module pages.
71
72You may define your own value types as described in
73\l{qtqml-cppintegration-definetypes.html}{Defining QML Types from C++}.
74In order to use types provided by a particular QML module, clients
75must import that module in their QML documents.
76
77\section1 Property Change Behavior for Value Types
78
79Some value types have properties: for example, the \l font type has
80\c pixelSize, \c family and \c bold properties. Unlike properties of
81\l{qtqml-typesystem-topic.html#qml-object-types}{object types}, properties of
82value types do not provide their own property change signals. It is only possible
83to create a property change signal handler for the value type property itself:
84
85\code
86Text {
87 // invalid!
88 onFont.pixelSizeChanged: doSomething()
89
90 // also invalid!
91 font {
92 onPixelSizeChanged: doSomething()
93 }
94
95 // but this is ok
96 onFontChanged: doSomething()
97}
98\endcode
99
100Be aware, however, that a property change signal for a value type is emitted
101whenever \e any of its attributes have changed, as well as when the property itself
102changes. Take the following code, for example:
103
104\qml
105Text {
106 onFontChanged: console.log("font changed")
107
108 Text { id: otherText }
109
110 focus: true
111
112 // changing any of the font attributes, or reassigning the property
113 // to a different font value, will invoke the onFontChanged handler
114 Keys.onDigit1Pressed: font.pixelSize += 1
115 Keys.onDigit2Pressed: font.b = !font.b
116 Keys.onDigit3Pressed: font = otherText.font
117}
118\endqml
119
120In contrast, properties of an \l{qtqml-typesystem-topic.html#qml-object-types}{object type}
121emit their own property change signals, and a property change signal handler for an object-type
122property is only invoked when the property is reassigned to a different object value.
123
124*/
125
126/*!
127 \qmlvaluetype int
128 \ingroup qmlvaluetypes
129 \brief a whole number, e.g. 0, 10, or -20.
130
131 The \c int type refers to a whole number, e.g. 0, 10, or -20.
132
133 The possible \c int values range from -2147483648 to 2147483647,
134 although most types will only accept a reduced range (which they
135 mention in their documentation).
136
137 Example:
138 \qml
139 NumberAnimation { loops: 5 }
140 \endqml
141
142 This value type is provided by the QML language.
143
144 \sa {QML Value Types}
145*/
146
147/*!
148 \qmlvaluetype bool
149 \ingroup qmlvaluetypes
150 \brief a binary true/false value.
151
152 The \c bool type refers to a binary true/false value.
153
154 Properties of type \c bool have \c false as their default value.
155
156 Example:
157 \qml
158 Item {
159 focus: true
160 clip: false
161 }
162 \endqml
163
164 This value type is provided by the QML language.
165
166 \sa {QML Value Types}
167*/
168
169/*!
170 \qmlvaluetype real
171 \ingroup qmlvaluetypes
172
173 \brief a number with a decimal point.
174
175 The \c real type refers to a number with decimal point, e.g. 1.2 or -29.8.
176
177 Example:
178 \qml
179 Item { width: 100.45; height: 150.82 }
180 \endqml
181
182 \note In QML all reals are stored in double precision, \l
183 {http://en.wikipedia.org/wiki/IEEE_754} {IEEE floating point}
184 format.
185
186 This value type is provided by the QML language.
187
188 \sa {QML Value Types}
189*/
190
191/*!
192 \qmlvaluetype double
193 \ingroup qmlvaluetypes
194
195 \brief a number with a decimal point, stored in double precision.
196
197 The \c double type refers to a number with a decimal point and is stored in double precision, \l
198 {http://en.wikipedia.org/wiki/IEEE_754} {IEEE floating point} format. It's the same as \c real.
199
200 Properties of type \c double have \e {0.0} as their default value.
201
202 Example:
203 \qml
204 Item {
205 property double number: 32155.2355
206 }
207 \endqml
208
209 This value type is provided by the QML language.
210
211 \sa {QML Value Types}
212*/
213
214/*!
215 \qmlvaluetype string
216 \ingroup qmlvaluetypes
217 \brief A free form text string.
218
219 The \c string type refers to a free form text string in quotes, for example
220 "Hello world!". The QML language provides this value type by default.
221
222 Example:
223 \qml
224 Text { text: "Hello world!" }
225 \endqml
226
227 Properties of type \c string are empty by default.
228
229 Strings have a \c length attribute that holds the number of characters in
230 the string.
231
232 The string value type is backed by the C++ type QString. It extends the
233 JavaScript String primitive type in that it provides much of the same API,
234 plus some extra methods. For example, the QML string value type method
235 \c {arg()} supports value substitution:
236
237 \qml
238 var message = "There are %1 items"
239 var count = 20
240 console.log(message.arg(count))
241 \endqml
242
243 The example above prints "There are 20 items".
244
245 The QML string value type supports most of the ECMAScript string features,
246 such as template (string) literals, string interpolation, multi-line
247 strings, and looping over strings.
248
249 In general, QML string supports most JavaScript String methods, including
250 checking for inclusion using \c string.includes(), \c string.startsWith(),
251 and \c string.endsWith(); repeating a string using \c string.repeats(), and
252 slicing and splitting using \c string.slice() and \c string.split().
253
254 For more information about which version of ECMAScript QML supports, see
255 \l {JavaScript Host Environment}
256
257 For more information about JavaScript String methods, see
258 \l {https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String}
259 {mdn JavaScript String}
260
261 When integrating with C++, note that any QString value
262 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is
263 automatically converted into a \c string value, and vice-versa.
264
265 \sa {QML Value Types}, {ECMA-262}{ECMAScript Language Specification}
266*/
267
268/*!
269 \qmlvaluetype url
270 \ingroup qmlvaluetypes
271 \brief a resource locator.
272
273 The \c url type refers to a resource locator (like a file name, for example). It can be either
274 absolute, e.g. "http://qt-project.org", or relative, e.g. "pics/logo.png". A relative URL is
275 resolved relative to the URL of the containing component.
276
277 For example, the following assigns a valid URL to the \l {Image::source}
278 property, which is of type \c url:
279
280 \qml
281 Image { source: "pics/logo.png" }
282 \endqml
283
284 When integrating with C++, note that any QUrl value
285 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
286 converted into a \c url value, and vice-versa.
287
288 Alternatively you may convert your \c url to a \l{https://developer.mozilla.org/en-US/docs/Web/API/URL}{URL} object
289 in order to access and modify its components:
290 \qml
291 var urlObject = new URL(url);
292 \endqml
293
294 \note In Qt 5, URLs were automatically resolved based on the current context
295 when assigning them to any \c url property. This made it impossible to
296 work with relative URLs and it created inconsistent behavior when reading
297 back a URL previously written to a property. Therefore, the behavior was
298 changed in Qt 6: URLs are not automatically resolved on assignment anymore.
299 The individual elements that use URLs have to resolve them themselves.
300
301 \note When referring to files stored with the \l{resources.html}{Qt Resource System}
302 from within QML, you should use "qrc:///" instead of ":/" as QML requires URL paths.
303 Relative URLs resolved from within that file will use the same protocol.
304
305 Additionally, URLs may contain encoded characters using the 'percent-encoding' scheme
306 specified by \l {https://datatracker.ietf.org/doc/html/rfc3986}{RFC 3986}. These characters
307 will be preserved within properties of type \c url, to allow QML code to
308 construct precise URL values.
309
310 For example, a local file containing a '#' character, which would normally be
311 interpreted as the beginning of the URL 'fragment' element, can be accessed by
312 encoding the characters of the file name:
313
314 \qml
315 Image { source: encodeURIComponent("/tmp/test#1.png") }
316 \endqml
317
318 This value type is provided by the QML language.
319
320 \sa {QML Value Types}
321*/
322
323
324/*!
325 \qmlvaluetype list
326 \ingroup qmlvaluetypes
327 \brief a list of QML objects.
328
329 The \c list type refers to a list of QML objects or values.
330
331 Properties of type \c list are empty by default.
332
333 A \c list can store QML objects or \l{QML Value Types}{value type} values.
334
335 When integrating with C++, note that any QQmlListProperty value
336 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
337 converted into a \c list value, and vice-versa.
338
339 Similarly any \c{QList<T>} of a registered value type \c{T} is automatically
340 converted into a \c list value, and vice-versa.
341
342 \section1 Using the list Type
343
344 For example, the \l Item type has a \l {Item::}{states} list-type property that
345 can be assigned to and used as follows:
346
347 \qml
348 import QtQuick
349
350 Item {
351 width: 100; height: 100
352
353 states: [
354 State { name: "activated" },
355 State { name: "deactivated" }
356 ]
357
358 Component.onCompleted: {
359 console.log("Name of first state:", states[0].name)
360 for (var i = 0; i < states.length; i++)
361 console.log("state", i, states[i].name)
362 }
363 }
364 \endqml
365
366 The defined \l State objects will be added to the \c states list
367 in the order in which they are defined.
368
369 If the list only contains one object, the square brackets may be omitted:
370
371 \qml
372 import QtQuick
373
374 Item {
375 width: 100; height: 100
376 states: State { name: "activated" }
377 }
378 \endqml
379
380 You can also declare your own list properties in QML:
381
382 \qml
383 import QtQml
384
385 QtObject {
386 property list<int> intList: [1, 2, 3, 4]
387 property list<QtObject> objectList
388 }
389 \endqml
390
391 Lists can be used much like JavaScript arrays. For example:
392
393 \list
394 \li Values are assigned using the \c[] square bracket syntax with comma-separated values
395 \li The \c length property provides the number of items in the list
396 \li Values in the list are accessed using the \c [index] syntax
397 \li You can use \c{push()} to append entries
398 \li You can set the \c length property of the list to truncate or extend it.
399 \endlist
400
401 However, you can \e{not} automatically extend the list by assigning to an
402 index currently out of range. Furthermore, if you insert \c null values
403 into a list of objects, those are converted to \c nullptr entries in
404 the underlying QQmlListProperty.
405
406 A list of value types is different from a JavaScript array in one further
407 important aspect: Growing it by setting its length does not produce undefined
408 entries, but rather default-constructed instances of the value type.
409
410 Similarly, growing a list of object types this way produces null entries,
411 rather than undefined entries.
412
413 This value type is provided by the QML language.
414
415 \sa {QML Value Types}
416*/
417
418 /*!
419 \qmlvaluetype var
420 \ingroup qmlvaluetypes
421 \brief a generic property type.
422
423 The \c var type is a generic property type that can refer to any data type.
424
425 It is equivalent to a regular JavaScript variable.
426 For example, var properties can store numbers, strings, objects,
427 arrays and functions:
428
429 \qml
430 Item {
431 property var aNumber: 100
432 property var aBool: false
433 property var aString: "Hello world!"
434 property var anotherString: String("#FF008800")
435 property var aColor: Qt.rgba(0.2, 0.3, 0.4, 0.5)
436 property var aRect: Qt.rect(10, 10, 10, 10)
437 property var aPoint: Qt.point(10, 10)
438 property var aSize: Qt.size(10, 10)
439 property var aVector3d: Qt.vector3d(100, 100, 100)
440 property var anArray: [1, 2, 3, "four", "five", (function() { return "six"; })]
441 property var anObject: { "foo": 10, "bar": 20 }
442 property var aFunction: (function() { return "one"; })
443 }
444 \endqml
445
446 \section1 Change Notification Semantics
447
448 It is important to note that changes in regular properties of JavaScript
449 objects assigned to a var property will \b{not} trigger updates of bindings
450 that access them. The example below will display "The car has 4 wheels" as
451 the change to the wheels property will not cause the reevaluation of the
452 binding assigned to the "text" property:
453
454 \qml
455 Item {
456 property var car: new Object({wheels: 4})
457
458 Text {
459 text: "The car has " + car.wheels + " wheels";
460 }
461
462 Component.onCompleted: {
463 car.wheels = 6;
464 }
465 }
466 \endqml
467
468 If the onCompleted handler instead had \tt{"car = new Object({wheels: 6})"}
469 then the text would be updated to say "The car has 6 wheels", since the
470 car property itself would be changed, which causes a change notification
471 to be emitted.
472
473 \section1 Property Value Initialization Semantics
474
475 The QML syntax defines that curly braces on the right-hand-side of a
476 property value initialization assignment denote a binding assignment.
477 This can be confusing when initializing a \c var property, as empty curly
478 braces in JavaScript can denote either an expression block or an empty
479 object declaration. If you wish to initialize a \c var property to an
480 empty object value, you should wrap the curly braces in parentheses.
481
482 Properties of type \c var are \c {undefined} by default.
483
484 For example:
485 \qml
486 Item {
487 property var first: {} // nothing = undefined
488 property var second: {{}} // empty expression block = undefined
489 property var third: ({}) // empty object
490 }
491 \endqml
492
493 In the previous example, the \c first property is bound to an empty
494 expression, whose result is undefined. The \c second property is bound to
495 an expression which contains a single, empty expression block ("{}"), which
496 similarly has an undefined result. The \c third property is bound to an
497 expression which is evaluated as an empty object declaration, and thus the
498 property will be initialized with that empty object value.
499
500 Similarly, a colon in JavaScript can be either an object property value
501 assignment, or a code label. Thus, initializing a var property with an
502 object declaration can also require parentheses:
503
504 \qml
505 Item {
506 property var first: { example: 'true' } // example is interpreted as a label
507 property var second: ({ example: 'true' }) // example is interpreted as a property
508 property var third: { 'example': 'true' } // example is interpreted as a property
509 Component.onCompleted: {
510 console.log(first.example) // prints 'undefined', as "first" was assigned a string
511 console.log(second.example) // prints 'true'
512 console.log(third.example) // prints 'true'
513 }
514 }
515 \endqml
516
517 \sa {QML Value Types}
518*/
519
520/*!
521 \qmlvaluetype variant
522 \ingroup qmlvaluetypes
523 \brief a generic property type.
524
525 The \c variant type is the same as the \c var type. Use \c var instead.
526
527 \sa {QML Value Types}
528*/
529
530/*!
531 \qmlvaluetype void
532 \ingroup qmlvaluetypes
533 \brief The empty value type.
534
535 The \c void type is exclusively used to type-annotate JavaScript functions
536 returning \c undefined. For example:
537
538 \qml
539 function doThings() : void { console.log("hello") }
540 \endqml
541
542 This is to help tooling analyze calls to such functions and compile them and
543 their callers to C++.
544
545 You cannot declare \c void properties in QML.
546
547 \sa {QML Value Types}
548*/
549
550/*!
551 \qmlvaluetype regex
552 \ingroup qmlvaluetypes
553 \internal
554 \brief Represents regular expressions.
555
556 \sa {QML Value Types}
557*/