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