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)) // "There are 20 items"
241 \endqml
242
243 \since 6.12
244 The \c {arg()} method also supports multiple arguments, allowing you to
245 replace multiple placeholders in a single call:
246
247 \qml
248 var message = "%1 of %2 files copied. Errors: %3"
249 console.log(message.arg(5, 7, 0)) // "5 of 7 files copied. Errors: 0"
250 \endqml
251
252 Using multiple arguments is more efficient than chaining \c {arg()} calls
253 and protects against issues when arguments themselves contain placeholders:
254
255 \qml
256 var result = "%1 %2".arg("%1f", "Hello") // "%1f Hello" (correct)
257 // Compare with chained calls:
258 var result = "%1 %2".arg("%1f").arg("Hello") // "Hellof %2" (incorrect)
259 \endqml
260
261 The QML string value type supports most of the ECMAScript string features,
262 such as template (string) literals, string interpolation, multi-line
263 strings, and looping over strings.
264
265 In general, QML string supports most JavaScript String methods, including
266 checking for inclusion using \c string.includes(), \c string.startsWith(),
267 and \c string.endsWith(); repeating a string using \c string.repeats(), and
268 slicing and splitting using \c string.slice() and \c string.split().
269
270 For more information about which version of ECMAScript QML supports, see
271 \l {JavaScript Host Environment}
272
273 For more information about JavaScript String methods, see
274 \l {https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String}
275 {mdn JavaScript String}
276
277 When integrating with C++, note that any QString value
278 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is
279 automatically converted into a \c string value, and vice-versa.
280
281 \sa {QML Value Types}, {ECMA-262}{ECMAScript Language Specification}
282*/
283
284/*!
285 \qmlvaluetype url
286 \ingroup qmlvaluetypes
287 \brief a resource locator.
288
289 The \c url type refers to a resource locator (like a file name, for example). It can be either
290 absolute, e.g. "http://qt-project.org", or relative, e.g. "pics/logo.png". A relative URL is
291 resolved relative to the URL of the containing component.
292
293 For example, the following assigns a valid URL to the \l {Image::source}
294 property, which is of type \c url:
295
296 \qml
297 Image { source: "pics/logo.png" }
298 \endqml
299
300 When integrating with C++, note that any QUrl value
301 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
302 converted into a \c url value, and vice-versa.
303
304 Alternatively you may convert your \c url to a \l{https://developer.mozilla.org/en-US/docs/Web/API/URL}{URL} object
305 in order to access and modify its components:
306 \qml
307 var urlObject = new URL(url);
308 \endqml
309
310 \note In Qt 5, URLs were automatically resolved based on the current context
311 when assigning them to any \c url property. This made it impossible to
312 work with relative URLs and it created inconsistent behavior when reading
313 back a URL previously written to a property. Therefore, the behavior was
314 changed in Qt 6: URLs are not automatically resolved on assignment anymore.
315 The individual elements that use URLs have to resolve them themselves.
316
317 \note When referring to files stored with the \l{resources.html}{Qt Resource System}
318 from within QML, you should use "qrc:///" instead of ":/" as QML requires URL paths.
319 Relative URLs resolved from within that file will use the same protocol.
320
321 Additionally, URLs may contain encoded characters using the 'percent-encoding' scheme
322 specified by \l {https://datatracker.ietf.org/doc/html/rfc3986}{RFC 3986}. These characters
323 will be preserved within properties of type \c url, to allow QML code to
324 construct precise URL values.
325
326 For example, a local file containing a '#' character, which would normally be
327 interpreted as the beginning of the URL 'fragment' element, can be accessed by
328 encoding the characters of the file name:
329
330 \qml
331 Image { source: encodeURIComponent("/tmp/test#1.png") }
332 \endqml
333
334 This value type is provided by the QML language.
335
336 \sa {QML Value Types}
337*/
338
339
340/*!
341 \qmlvaluetype list
342 \ingroup qmlvaluetypes
343 \brief a list of QML objects.
344
345 The \c list type refers to a list of QML objects or values.
346
347 Properties of type \c list are empty by default.
348
349 A \c list can store QML objects or \l{QML Value Types}{value type} values.
350
351 When integrating with C++, note that any QQmlListProperty value
352 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
353 converted into a \c list value, and vice-versa.
354
355 Similarly any \c{QList<T>} of a registered value type \c{T} is automatically
356 converted into a \c list value, and vice-versa.
357
358 \section1 Using the list Type
359
360 For example, the \l Item type has a \l {Item::}{states} list-type property that
361 can be assigned to and used as follows:
362
363 \qml
364 import QtQuick
365
366 Item {
367 width: 100; height: 100
368
369 states: [
370 State { name: "activated" },
371 State { name: "deactivated" }
372 ]
373
374 Component.onCompleted: {
375 console.log("Name of first state:", states[0].name)
376 for (var i = 0; i < states.length; i++)
377 console.log("state", i, states[i].name)
378 }
379 }
380 \endqml
381
382 The defined \l State objects will be added to the \c states list
383 in the order in which they are defined.
384
385 If the list only contains one object, the square brackets may be omitted:
386
387 \qml
388 import QtQuick
389
390 Item {
391 width: 100; height: 100
392 states: State { name: "activated" }
393 }
394 \endqml
395
396 You can also declare your own list properties in QML:
397
398 \qml
399 import QtQml
400
401 QtObject {
402 property list<int> intList: [1, 2, 3, 4]
403 property list<QtObject> objectList
404 }
405 \endqml
406
407 Lists can be used much like JavaScript arrays. For example:
408
409 \list
410 \li Values are assigned using the \c[] square bracket syntax with comma-separated values
411 \li The \c length property provides the number of items in the list
412 \li Values in the list are accessed using the \c [index] syntax
413 \li You can use \c{push()} to append entries
414 \li You can set the \c length property of the list to truncate or extend it.
415 \endlist
416
417 However, you can \e{not} automatically extend the list by assigning to an
418 index currently out of range. Furthermore, if you insert \c null values
419 into a list of objects, those are converted to \c nullptr entries in
420 the underlying QQmlListProperty.
421
422 A list of value types is different from a JavaScript array in one further
423 important aspect: Growing it by setting its length does not produce undefined
424 entries, but rather default-constructed instances of the value type.
425
426 Similarly, growing a list of object types this way produces null entries,
427 rather than undefined entries.
428
429 This value type is provided by the QML language.
430
431 \sa {QML Value Types}
432*/
433
434 /*!
435 \qmlvaluetype var
436 \ingroup qmlvaluetypes
437 \brief a generic property type.
438
439 The \c var type is a generic property type that can refer to any data type.
440
441 It is equivalent to a regular JavaScript variable.
442 For example, var properties can store numbers, strings, objects,
443 arrays and functions:
444
445 \qml
446 Item {
447 property var aNumber: 100
448 property var aBool: false
449 property var aString: "Hello world!"
450 property var anotherString: String("#FF008800")
451 property var aColor: Qt.rgba(0.2, 0.3, 0.4, 0.5)
452 property var aRect: Qt.rect(10, 10, 10, 10)
453 property var aPoint: Qt.point(10, 10)
454 property var aSize: Qt.size(10, 10)
455 property var aVector3d: Qt.vector3d(100, 100, 100)
456 property var anArray: [1, 2, 3, "four", "five", (function() { return "six"; })]
457 property var anObject: { "foo": 10, "bar": 20 }
458 property var aFunction: (function() { return "one"; })
459 }
460 \endqml
461
462 \section1 Change Notification Semantics
463
464 It is important to note that changes in regular properties of JavaScript
465 objects assigned to a var property will \b{not} trigger updates of bindings
466 that access them. The example below will display "The car has 4 wheels" as
467 the change to the wheels property will not cause the reevaluation of the
468 binding assigned to the "text" property:
469
470 \qml
471 Item {
472 property var car: new Object({wheels: 4})
473
474 Text {
475 text: "The car has " + car.wheels + " wheels";
476 }
477
478 Component.onCompleted: {
479 car.wheels = 6;
480 }
481 }
482 \endqml
483
484 If the onCompleted handler instead had \tt{"car = new Object({wheels: 6})"}
485 then the text would be updated to say "The car has 6 wheels", since the
486 car property itself would be changed, which causes a change notification
487 to be emitted.
488
489 \section1 Property Value Initialization Semantics
490
491 The QML syntax defines that curly braces on the right-hand-side of a
492 property value initialization assignment denote a binding assignment.
493 This can be confusing when initializing a \c var property, as empty curly
494 braces in JavaScript can denote either an expression block or an empty
495 object declaration. If you wish to initialize a \c var property to an
496 empty object value, you should wrap the curly braces in parentheses.
497
498 Properties of type \c var are \c {undefined} by default.
499
500 For example:
501 \qml
502 Item {
503 property var first: {} // nothing = undefined
504 property var second: {{}} // empty expression block = undefined
505 property var third: ({}) // empty object
506 }
507 \endqml
508
509 In the previous example, the \c first property is bound to an empty
510 expression, whose result is undefined. The \c second property is bound to
511 an expression which contains a single, empty expression block ("{}"), which
512 similarly has an undefined result. The \c third property is bound to an
513 expression which is evaluated as an empty object declaration, and thus the
514 property will be initialized with that empty object value.
515
516 Similarly, a colon in JavaScript can be either an object property value
517 assignment, or a code label. Thus, initializing a var property with an
518 object declaration can also require parentheses:
519
520 \qml
521 Item {
522 property var first: { example: 'true' } // example is interpreted as a label
523 property var second: ({ example: 'true' }) // example is interpreted as a property
524 property var third: { 'example': 'true' } // example is interpreted as a property
525 Component.onCompleted: {
526 console.log(first.example) // prints 'undefined', as "first" was assigned a string
527 console.log(second.example) // prints 'true'
528 console.log(third.example) // prints 'true'
529 }
530 }
531 \endqml
532
533 \sa {QML Value Types}
534*/
535
536/*!
537 \qmlvaluetype variant
538 \ingroup qmlvaluetypes
539 \brief a generic property type.
540
541 The \c variant type is the same as the \c var type. Use \c var instead.
542
543 \sa {QML Value Types}
544*/
545
546/*!
547 \qmlvaluetype void
548 \ingroup qmlvaluetypes
549 \brief The empty value type.
550
551 The \c void type is exclusively used to type-annotate JavaScript functions
552 returning \c undefined. For example:
553
554 \qml
555 function doThings() : void { console.log("hello") }
556 \endqml
557
558 This is to help tooling analyze calls to such functions and compile them and
559 their callers to C++.
560
561 You cannot declare \c void properties in QML.
562
563 \sa {QML Value Types}
564*/
565
566/*!
567 \qmlvaluetype regex
568 \ingroup qmlvaluetypes
569 \internal
570 \brief Represents regular expressions.
571
572 \sa {QML Value Types}
573*/