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