1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** GNU Free Documentation License
10 ** Alternatively, this file may be used under the terms of the GNU Free
11 ** Documentation License version 1.3 as published by the Free Software
12 ** Foundation and appearing in the file included in the packaging of
16 ** Alternatively, this file may be used in accordance with the terms
17 ** and conditions contained in a signed written agreement between you
26 ****************************************************************************/
29 \page qml-basictypes.html
31 \title QML Basic Types
32 \brief basic data types in QML
34 QML has a set of primitive types, as listed below, that are used throughout
35 the \l {QML Elements}.
37 \annotatedlist qmlbasictypes
39 To create additional types, such as data types created in C++, read the
40 \l{Creating QML Types} article.
45 \ingroup qmlbasictypes
47 \brief An integer is a whole number, e.g. 0, 10, or -20.
49 An integer is a whole number, e.g. 0, 10, or -20. The possible \c
50 int values range from around -2000000000 to around 2000000000,
51 although most elements will only accept a reduced range (which they
52 mention in their documentation).
56 Item { width: 100; height: 200 }
64 \ingroup qmlbasictypes
66 \brief A boolean is a binary true/false value.
68 A boolean is a binary true/false value.
72 Item { focus: true; clip: false }
80 \ingroup qmlbasictypes
82 \brief A real number has a decimal point, e.g. 1.2 or -29.8.
84 A real number has a decimal point, e.g. 1.2 or -29.8.
88 Item { width: 100.45; height: 150.82 }
91 \bold{Note:} In QML all reals are stored in double precision, \l
92 {http://en.wikipedia.org/wiki/IEEE_754} {IEEE floating point}
100 \ingroup qmlbasictypes
102 \brief A double number has a decimal point and is stored in double precision.
104 A double number has a decimal point and is stored in double precision, \l
105 {http://en.wikipedia.org/wiki/IEEE_754} {IEEE floating point}
111 property double number: 32155.2355
115 \sa {QML Basic Types}
120 \ingroup qmlbasictypes
122 \brief A string is a free form text in quotes, e.g. "Hello world!".
124 A string is a free form text in quotes, e.g. "Hello world!".
128 Text { text: "Hello world!" }
131 Strings have a \c length attribute that holds the number of
132 characters in the string.
134 \sa {QML Basic Types}
139 \ingroup qmlbasictypes
141 \brief A URL is a resource locator, like a file name.
143 A URL is a resource locator, like a file name. It can be either
144 absolute, e.g. "http://qt.nokia.com", or relative, e.g.
145 "pics/logo.png". A relative URL is resolved relative to the URL of
146 the component where the URL is converted from a JavaScript string
147 expression to a url property value.
151 Image { source: "pics/logo.png" }
154 Note that as QML requires URL paths, you should use "qrc:///" instead of ":/" for
155 referring to files stored with the \l{resources.html}{Qt Resource System}.
156 Usually you will only have to do this once, because relative URLs resolved from
157 that file will use the same protocol.
159 \sa {QML Basic Types}
164 \ingroup qmlbasictypes
166 \brief A color is a standard color name in quotes.
168 A color is a standard color name in quotes. It is normally specified
169 as an \l {http://www.w3.org/TR/SVG/types.html#ColorKeywords} {SVG
170 color name}. These names include colors like "red", "green" and
173 If the color you want isn't part of this list, colors can also be
174 specified in hexidecimal triplets or quads that take the form \c
175 "#RRGGBB" and \c "#AARRGGBB" respectively. For example, the color
176 red corresponds to a triplet of \c "#FF0000" and a slightly
177 transparent blue to a quad of \c "#800000FF".
181 \inlineimage declarative-colors.png
183 \snippet doc/src/snippets/qml/colors.qml colors
185 Or with the \l{QML:Qt::rgba()}{Qt.rgba()}, \l{QML:Qt::hsla()}{Qt.hsla()}, \l{QML:Qt::darker()}{Qt.darker()},
186 \l{QML:Qt::lighter()}{Qt.lighter()} or \l{QML:Qt::tint()}{Qt.tint()} functions:
189 Rectangle { color: Qt.rgba(0.5, 0.5, 0, 1) }
192 \sa {QML Basic Types}
197 \ingroup qmlbasictypes
199 \brief A point type has x and y attributes.
201 A \c point type has \c x and \c y attributes.
203 To create a \c point value, specify it as a "x,y" string:
206 CustomObject { myPointProperty: "0,20" }
209 Or use the \l{QML:Qt::point()}{Qt.point()} function:
212 CustomObject { myPointProperty: Qt.point(0, 20) }
215 \sa {QML Basic Types}
220 \ingroup qmlbasictypes
222 \brief A size type has width and height attributes
224 A \c size type has \c width and \c height attributes.
226 For example, to read the \l {Image::sourceSize} \c size property:
230 Image { id: image; source: "logo.png" }
231 Text { text: image.sourceSize.width + "," + image.sourceSize.height }
235 To create a \c size value, specify it as a "width x height" string:
238 LayoutItem { preferredSize: "150x50" }
241 Or use the \l{QML:Qt::size()}{Qt.size()} function:
244 LayoutItem { preferredSize: Qt.size(150, 50) }
247 \sa {QML Basic Types}
252 \ingroup qmlbasictypes
254 \brief A rect type has x, y, width and height attributes.
256 A \c rect type has \c x, \c y, \c width and \c height attributes.
258 For example, to read the \l {Item::childrenRect.x}{Item::childrenRect} \c rect property:
261 width: childrenRect.width
262 height: childrenRect.height
264 Rectangle { width: 100; height: 100 }
268 To create a \c rect value, specify it as a "x, y, width x height" string:
271 CustomObject { myRectProperty: "50,50,100x100" }
274 Or use the \l{QML:Qt::rect()}{Qt.rect()} function:
277 CustomObject { myRectProperty: Qt.rect(50, 50, 100, 100) }
280 \sa {QML Basic Types}
285 \ingroup qmlbasictypes
287 \brief A date is specified as "YYYY-MM-DD".
289 To create a \c date value, specify it as a "YYYY-MM-DD" string:
293 MyDatePicker { minDate: "2000-01-01"; maxDate: "2020-12-31" }
296 To read a date value returned from a C++ extension class, use
297 \l{QML:Qt::formatDate()}{Qt.formatDate()} and \l{QML:Qt::formatDateTime()}{Qt.formatDateTime()}.
299 \sa {QML Basic Types}
304 \ingroup qmlbasictypes
306 \brief A time is specified as "hh:mm:ss".
308 A time is specified as "hh:mm:ss".
312 MyTimePicker { time: "14:22:15" }
315 To read a time value returned from a C++ extension class, use
316 \l{QML:Qt::formatTime()}{Qt.formatTime()} and \l{QML:Qt::formatDateTime()}{Qt.formatDateTime()}.
318 Note that when converting historical times to and from javascript that QDateTime and the JS Date object
319 have different methods of calculating historical daylight savings time application. This can lead to variations of one hour
320 when converting to historical local time.
322 \sa {QML Basic Types}
327 \ingroup qmlbasictypes
329 \brief A font type has the properties of a QFont.
331 A font type has the properties of a QFont. The properties are:
334 \o \c string font.family
336 \o \c bool font.italic
337 \o \c bool font.underline
338 \o \c real font.pointSize
339 \o \c int font.pixelSize
344 Text { font.family: "Helvetica"; font.pointSize: 13; font.bold: true }
347 \sa {QML Basic Types}
352 \ingroup qmlbasictypes
354 \brief The action type has all the properties of QAction.
356 The action type has all the properties of QAction. The properties
360 \o \c slot action.trigger - invoke the action
361 \o \c bool action.enabled - true if the action is enabled
362 \o \c string action.text - the text associated with the action
365 Actions are used like this:
369 MouseArea { onClicked: myaction.trigger() }
370 State { name: "enabled"; when: myaction.enabled == true }
371 Text { text: someaction.text }
375 \sa {QML Basic Types}
380 \ingroup qmlbasictypes
382 \brief A list of objects.
384 A list type contains a list of objects. While not technically
385 a basic type, QML supports lists of object types. When used
386 from QML, the engine automatically appends each value to the list.
387 Items in the list can be accessed by index using the usual
388 \c listName[index] syntax.
390 For example, the \l Item class contains a list property named
391 children that can be used like this:
397 Rectangle { id: child2; width: 200 },
401 Component.onCompleted: {
402 console.log("Width of child rectangle:", children[1].width)
406 \c child1, \c child2 and \c child3 will be added to the children list
407 in the order in which they appear.
409 List \l {Property Binding}{properties} can be created as a
410 \c variant type, or as a \c list<Type> type, where \c Type is the
411 type of the object in the list:
415 property list<Rectangle> rects: [
416 Rectangle { width: 100; height: 100},
417 Rectangle { width: 200; height: 200}
422 A list property can only contain values that match (or are derived from) the
425 While the \c rects property can be reassigned to a different list value (including
426 an empty list), its individual values cannot be modified. See the \l variant type
427 documentation for details.
429 \sa {QML Basic Types}
434 \ingroup qmlbasictypes
436 \brief A var type is a generic property type.
438 A var is a generic property type capable of storing any data type.
439 It is equivalent to a regular JavaScript variable, except that you
440 cannot assign a JavaScript function to such a property.
441 For example, var properties can store numbers, strings, objects and
446 property var aNumber: 100
447 property var aBool: false
448 property var aString: "Hello world!"
449 property var anotherString: String("#FF008800")
450 property var aColor: Qt.rgba(0.2, 0.3, 0.4, 0.5)
451 property var aRect: Qt.rect(10, 10, 10, 10)
452 property var aPoint: Qt.point(10, 10)
453 property var aSize: Qt.size(10, 10)
454 property var aVector3d: Qt.vector3d(100, 100, 100)
455 property var anArray: [1, 2, 3, "four", "five", (function() { return "six"; })]
456 property var anObject: { "foo": 10, "bar": 20 }
460 Attempting to assign a JavaScript function to a var property will result in
461 a binding assignment as per other property types. You can assign a JavaScript
462 array containing a single function element instead.
464 It is important to note that changes in regular properties of JavaScript
465 objects assigned to a var property will \bold{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:
472 property var car: new Object({wheels: 4})
475 text: "The car has " + car.wheels + " wheels";
478 Component.onCompleted: {
484 A \c var type property can also hold an image or pixmap.
485 A \c var which contains a QPixmap or QImage is known as a
486 "scarce resource" and the declarative engine will attempt to
487 automatically release such resources after evaluation of any JavaScript
488 expression which requires one to be copied has completed.
490 Clients may explicitly release such a scarce resource by calling the
491 "destroy" method on the \c var property from within JavaScript. They
492 may also explicitly preserve the scarce resource by calling the
493 "preserve" method on the \c var property from within JavaScript.
494 For more information regarding the usage of a scarce resource, please
495 see \l{Scarce Resources in JavaScript}.
497 \sa {QML Basic Types}
503 \qmlbasictype variant
504 \ingroup qmlbasictypes
506 \brief A variant type is a generic property type.
508 A variant is a generic property type. It is obsolete and exists only to
509 support old applications; new applications should use "var" type
512 A variant type property can hold any of the \l {QML Basic Types}{basic type}
517 property variant aNumber: 100
518 property variant aString: "Hello world!"
519 property variant aBool: false
523 A \c variant type property can also hold an image or pixmap.
524 A \c variant which contains a QPixmap or QImage is known as a
525 "scarce resource" and the declarative engine will attempt to
526 automatically release such resources after evaluation of any JavaScript
527 expression which requires one to be copied has completed.
529 Clients may explicitly release such a scarce resource by calling the
530 "destroy" method on the \c variant property from within JavaScript. They
531 may also explicitly preserve the scarce resource by calling the
532 "preserve" method on the \c variant property from within JavaScript.
533 For more information regarding the usage of a scarce resource, please
534 see \l{Scarce Resources in JavaScript}.
536 Finally, the \c variant type can also hold:
539 \o An array of \l {QML Basic Types}{basic type} values
540 \o A map of key-value pairs with \l {QML Basic Types}{basic-type} values
543 For example, below is an \c items array and an \c attributes map. Their
544 contents can be examined using JavaScript \c for loops. Individual array
545 values are accessible by index, and individual map values are accessible
550 property variant items: [1, 2, 3, "four", "five"]
551 property variant attributes: { 'color': 'red', 'width': 100 }
553 Component.onCompleted: {
554 for (var i=0; i<items.length; i++)
555 console.log(items[i])
557 for (var prop in attributes)
558 console.log(prop, "=", attributes[prop])
563 While this is a convenient way to store array and map-type values, you
564 must be aware that the \c items and \c attributes properties above are \i not
565 QML objects (and certainly not JavaScript object either) and the key-value
566 pairs in \c attributes are \i not QML properties. Rather, the \c items
567 property holds an array of values, and \c attributes holds a set of key-value
568 pairs. Since they are stored as a set of values, instead of as an object,
569 their contents \i cannot be modified individually:
573 property variant items: [1, 2, 3, "four", "five"]
574 property variant attributes: { 'color': 'red', 'width': 100 }
576 Component.onCompleted: {
578 console.log(items[0]) // This will still be '1'!
579 attributes.color = 'blue'
580 console.log(attributes.color) // This will still be 'red'!
585 Additionally, since \c items and \c attributes are not QML objects, changing
586 their individual values do not trigger property change notifications. If
587 the above example had \c onNumberChanged or \c onAnimalChanged signal
588 handlers, they would not have been called. If, however, the \c items or
589 \c attributes properties themselves were reassigned to different values, then
590 such handlers would be called.
592 One way to "update" the contents of an array or map is to copy the property
593 to a JavaScript object, modify the copy as desired, and then reassign the
594 property to the updated copy. Note, however, that this is not efficient.
595 In the example below, which reassigns the \c attributes property, the \i entire
596 set of key-value pairs must be serialized and deserialized every time it is
597 copied between a JavaScript object and a QML property:
601 property variant attributes: { 'color': 'red', 'width': 100 }
603 Component.onCompleted: {
604 // Change the value of attributes.color to 'blue':
605 var temp = attributes // copy all values to 'temp'
607 attributes = temp // copy all values back to 'attributes'
612 Since this operation is inefficient, if a list or map should be modifiable,
613 it is better to use alternative approaches. For example, you could implement
614 a custom C++ list element, or write to a JavaScript object defined from
615 within a JavaScript file.
617 JavaScript programmers should also note that when a JavaScript object is
618 copied to an array or map property, the \i contents of the object (that is,
619 its key-value properties) are copied, rather than the object itself. The
620 property does not hold a reference to the original JavaScript object, and
621 extra data such as the object's JavaScript prototype chain is also lost in
624 \sa {QML Basic Types}
628 \qmlbasictype vector3d
629 \ingroup qmlbasictypes
631 \brief A vector3d type has x, y, and z attributes.
633 A \c vector3d type has \c x, \c y, and \c z attributes.
635 To create a \c vector3d value, specify it as a "x,y,z" string:
638 Rotation { angle: 60; axis: "0,1,0" }
641 or with the \l{QML:Qt::vector3d()}{Qt.vector3d()} function:
644 Rotation { angle: 60; axis: Qt.vector3d(0, 1, 0) }
647 or as separate \c x, \c y, and \c z components:
650 Rotation { angle: 60; axis.x: 0; axis.y: 1; axis.z: 0 }
653 \sa {QML Basic Types}
657 \qmlbasictype enumeration
658 \ingroup qmlbasictypes
660 \brief An enumeration type consists of a set of named values.
662 An enumeration type consists of a set of named values.
664 An enumeration value may be specified as either a string:
666 Text { horizontalAlignment: "AlignRight" }
669 or as \c {<Element>.<value>}:
671 Text { horizontalAlignment: Text.AlignRight }
674 The second form is preferred.
676 \sa {QML Basic Types}