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 ****************************************************************************/
28 \page qtqml-basictypes.html
29 \title QML Basic Types
30 \brief Description of basic types provided by the Qt QML module
32 QML supports a number of basic types.
34 A \e{basic type} is one that refers to a simple value, such as an \c int
35 or a \c string. This contrasts with a \l{qtqml-typesystem-topic.html#qml-object-types}{QML Object Types},
36 which refers to an object with properties, signals, methods and so on. Unlike an object type,
37 a basic type cannot be used to declare QML objects: it is not possible, for example, to declare an
38 \c int{} object or a \c size{} object.
40 Basic types can be used to refer to:
43 \li A single value (e.g. \l int refers to a single number, \l var can refer to a single list of items)
44 \li A value that contains a simple set of property-value pairs (e.g. \l size refers to a value with \c width and \c height attributes)
47 \sa {qtqml-typesystem-topic.html}{The QML Type System}
50 \section1 Supported Basic Types
52 The basic types supported in QML are listed below:
54 \annotatedlist qmlbasictypes
57 \section1 Property Change Behavior for Basic Types
59 Some basic types have properties: for example, the \l font type has
60 \c pixelSize, \c family and \c bold properties. Unlike properties of
61 \l{qtqml-typesystem-topic.html#qml-object-types}{object types}, properties of
62 basic types do not provide their own property change signals. It is only possible
63 to create a property change signal handler for the basic type property itself:
68 onFont.pixelSizeChanged: doSomething()
72 onPixelSizeChanged: doSomething()
76 onFontChanged: doSomething()
80 Be aware, however, that a property change signal for a basic type is emitted
81 whenever \e any of its attributes have changed, as well as when the property itself
82 changes. Take the following code, for example:
86 onFontChanged: console.log("font changed")
88 Text { id: otherText }
92 // changing any of the font attributes, or reassigning the property
93 // to a different font value, will invoke the onFontChanged handler
94 Keys.onDigit1Pressed: font.pixelSize += 1
95 Keys.onDigit2Pressed: font.bold = !font.bold
96 Keys.onDigit3Pressed: font = otherText.font
100 In contrast, properties of an \l{qtqml-typesystem-topic.html#qml-object-types}{object type}
101 emit their own property change signals, and a property change signal handler for an object-type
102 property is only invoked when the property is reassigned to a different object value.
108 \ingroup qmlbasictypes
109 \brief a whole number, e.g. 0, 10, or -20.
111 The \c int type refers to a whole number, e.g. 0, 10, or -20.
113 The possible \c int values range from around -2000000000 to around 2000000000,
114 although most elements will only accept a reduced range (which they
115 mention in their documentation).
119 Item { width: 100; height: 200 }
122 \sa {QML Basic Types}
127 \ingroup qmlbasictypes
128 \brief a binary true/false value.
130 The \c bool type refers to a binary true/false value.
140 \sa {QML Basic Types}
145 \ingroup qmlbasictypes
147 \brief a number with a decimal point.
149 The \c real type refers to a number with decimal point, e.g. 1.2 or -29.8.
153 Item { width: 100.45; height: 150.82 }
156 \b{Note:} In QML all reals are stored in double precision, \l
157 {http://en.wikipedia.org/wiki/IEEE_754} {IEEE floating point}
160 \sa {QML Basic Types}
165 \ingroup qmlbasictypes
167 \brief a number with a decimal point, stored in double precision.
169 The \c double type refers to a number with a decimal point and is stored in double precision, \l
170 {http://en.wikipedia.org/wiki/IEEE_754} {IEEE floating point} format.
175 property double number: 32155.2355
179 \sa {QML Basic Types}
184 \ingroup qmlbasictypes
185 \brief a free form text string.
187 The \c string type refers to a free form text string in quotes, e.g. "Hello world!".
191 Text { text: "Hello world!" }
194 Strings have a \c length attribute that holds the number of
195 characters in the string.
197 QML extends the JavaScript String type with a \l {String::arg}{arg()} function
198 to support value substitution.
200 When integrating with C++, note that any QString value
201 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
202 converted into a \c string value, and vice-versa.
204 \sa {QML Basic Types}
209 \ingroup qmlbasictypes
210 \brief a resource locator.
212 The \c url type refers to a resource locator (like a file name, for example). It can be either
213 absolute, e.g. "http://qt.nokia.com", or relative, e.g. "pics/logo.png". A relative URL is
214 resolved relative to the URL of the containing component.
216 For example, the following assigns a valid URL to the \l {Image::source}
217 property, which is of type \c url:
220 Image { source: "pics/logo.png" }
223 When integrating with C++, note that any QUrl value
224 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
225 converted into a \c url value, and vice-versa.
228 \section1 Using the url type
230 When a relative URL is written to a \c url type property, it is converted
231 into a URL object, so \bold {matching the URL value against the input string
232 value will fail}. Instead, convert the string to a URL using Qt.resolvedUrl()
233 for means of comparison, and use \c toString() to get the contents of the URL:
237 source: "pics/logo.png"
239 Component.onCompleted: {
240 // This prints 'false'. Although "pics/logo.png" was the input string,
241 // it's been converted from a string to a URL, so these two are not the same.
242 console.log(source == "pics/logo.png")
244 // This prints 'true' as Qt.resovledUrl() converts the string into a
245 // URL with the correctly resolved path
246 console.log(source == Qt.resolvedUrl("pics/logo.png"))
248 // This prints the absolute path, e.g. "file:///path/to/pics/logo.png"
249 console.log(source.toString())
254 \note When referring to files stored with the \l{resources.html}{Qt Resource System}
255 from within QML, you should use "qrc:///" instead of ":/" as QML requires URL paths.
256 Relative URLs resolved from within that file will use the same protocol.
258 Additionally, URLs may contain encoded characters using the 'percent-encoding' scheme
259 specified by \l {http://tools.ietf.org/html/rfc3986}{RFC 3986}. These characters
260 will be preserved within properties of type \c url, to allow QML code to
261 construct precise URL values. An exception to this rule is the preemptive
262 decoding of directory-separator characters (\c '/') - these characters are decoded
263 to allow the URL to be correctly classified.
265 For example, a local file containing a '#' character, which would normally be
266 interpreted as the beginning of the URL 'fragment' element, can be accessed by
267 encoding the characters of the file name:
270 Image { source: encodeURIComponent("/tmp/test#1.png") }
273 \sa {QML Basic Types}
278 \ingroup qmlbasictypes
279 \brief an ARGB color value.
281 The \c color type refers to an ARGB color value. It can be specified in a number of ways:
284 \li By a \l{http://www.w3.org/TR/SVG/types.html#ColorKeywords}{SVG color name}, such as
285 "red", "green" or "lightsteelblue".
286 \li By a hexadecimal triplet or quad in the form \c "#RRGGBB" and \c "#AARRGGBB"
287 respectively. For example, the color red corresponds to a triplet of \c "#FF0000"
288 and a slightly transparent blue to a quad of \c "#800000FF".
289 \li Using the \l{QML:Qt::rgba()}{Qt.rgba()}, \l{QML:Qt::hsla()}{Qt.hsla()},
290 \l{QML:Qt::darker()}{Qt.darker()}, \l{QML:Qt::lighter()}{Qt.lighter()} or
291 \l{QML:Qt::tint()}{Qt.tint()} functions.
297 \inlineimage declarative-colors.png
299 \snippet qml/colors.qml colors
301 Additionally, a color type has \c r, \c g, \c b and \c a properties that refer to the
302 red, green, blue and alpha values of the color, respectively:
309 Component.onCompleted: console.log(color.r, color.g, color.b, color.a)
313 When integrating with C++, note that any QColor value
314 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
315 converted into a \c color value, and vice-versa.
317 \sa {QML Basic Types}
322 \ingroup qmlbasictypes
323 \brief a value with x and y attributes.
325 The \c point type refers to a value with \c x and \c y attributes.
327 To create a \c point value, specify it as a "x,y" string:
330 CustomObject { myPointProperty: "0,20" }
333 Or use the \l{QML:Qt::point()}{Qt.point()} function:
336 CustomObject { myPointProperty: Qt.point(0, 20) }
339 When integrating with C++, note that any QPoint or QPointF value
340 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
341 converted into a \c point value. When a \c point value is passed to C++, it
342 is automatically converted into a QPointF value.
344 \sa {QML Basic Types}
349 \ingroup qmlbasictypes
350 \brief a value with width and height attributes
352 The \c size type refers to a value with has \c width and \c height attributes.
354 For example, to read the \c width and \c height values of the
355 \l {Image::sourceSize} size-type property:
359 Image { id: image; source: "logo.png" }
360 Text { text: image.sourceSize.width + "," + image.sourceSize.height }
364 To create a \c size value, specify it as a "width x height" string:
367 Image { sourceSize: "150x50" }
370 Or use the \l{QML:Qt::size()}{Qt.size()} function:
373 Image { sourceSize: Qt.size(150, 50) }
376 When integrating with C++, note that any QSize or QSizeF value
377 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
378 converted into a \c size value, and vice-versa. When a \c size value is passed to C++, it
379 is automatically converted into a QSizeF value.
381 \sa {QML Basic Types}
386 \ingroup qmlbasictypes
387 \brief a value with x, y, width and height attributes.
389 The \c rect type refers to a value with \c x, \c y, \c width and \c height attributes.
391 For example, to read the \c width and \c height values of the \l Item
392 \l {Item::}{childrenRect} rect-type type property:
396 width: childrenRect.width
397 height: childrenRect.height
399 Rectangle { width: 100; height: 100 }
403 To create a \c rect value, specify it as a "x, y, width x height" string:
406 CustomObject { myRectProperty: "50,50,100x100" }
409 Or use the \l{QML:Qt::rect()}{Qt.rect()} function:
412 CustomObject { myRectProperty: Qt.rect(50, 50, 100, 100) }
415 When integrating with C++, note that any QRect or QRectF value
416 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
417 converted into a \c rect value, and vice-versa. When a \c rect value is passed to C++, it
418 is automatically converted into a QRectF value.
421 \sa {QML Basic Types}
426 \ingroup qmlbasictypes
429 The \c date type refers to a date value.
431 To create a \c date value, specify it as a "YYYY-MM-DD" string:
434 MyDatePicker { minDate: "2000-01-01"; maxDate: "2020-12-31" }
437 To read a date value returned from a C++ extension class, use
438 \l{QML:Qt::formatDate()}{Qt.formatDate()} and \l{QML:Qt::formatDateTime()}{Qt.formatDateTime()}.
440 When integrating with C++, note that any QDate value
441 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
442 converted into a \c date value, and vice-versa.
444 \sa {QML Basic Types}
449 \ingroup qmlbasictypes
452 The \c time type refers to a time value.
454 To create a \c time value, specified as "hh:mm:ss":
457 MyTimePicker { time: "14:22:15" }
460 To read a time value returned from a C++ extension class, use
461 \l{QML:Qt::formatTime()}{Qt.formatTime()} and \l{QML:Qt::formatDateTime()}{Qt.formatDateTime()}.
463 Note that when converting historical times to and from javascript that QDateTime and the JS Date object
464 have different methods of calculating historical daylight savings time application. This can lead to variations of one hour
465 when converting to historical local time.
467 When integrating with C++, note that any QTime value
468 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
469 converted into a \c time value, and vice-versa.
471 \sa {QML Basic Types}
476 \ingroup qmlbasictypes
477 \brief a font value with the properties of QFont.
479 The \c font type refers to a font value with the properties of QFont.
481 These properties are:
484 \li \l string \c font.family
485 \li \l bool \c font.bold
486 \li \l bool \c font.italic
487 \li \l bool \c font.underline
488 \li \l real \c font.pointSize
489 \li \l int \c font.pixelSize
494 Text { font.family: "Helvetica"; font.pointSize: 13; font.bold: true }
497 When integrating with C++, note that any QFont value
498 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
499 converted into a \c font value, and vice-versa.
501 \sa {QML Basic Types}
506 \ingroup qmlbasictypes
507 \brief a list of QML objects.
509 The \c list type refers to a list of QML objects.
511 A list value can be accessed in a similar way to a JavaScript array:
514 \li Values are assigned using the \c[] square bracket syntax with comma-separated values
515 \li The \c length property provides the number of items in the list
516 \li Values in the list are accessed using the \c [index] syntax
519 A \c list can only store QML objects, and cannot contain any
520 \l {QML Basic Types}{basic type} values. (To store basic types within a
521 list, use the \l var type instead.)
523 When integrating with C++, note that any QQmlListProperty value
524 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
525 converted into a \c list value, and vice-versa.
528 \section1 Using the list type
530 For example, the \l Item type has a \l {Item::}{states} list-type property that
531 can be assigned to and used as follows:
537 width: 100; height: 100
540 State { name: "activated" },
541 State { name: "deactivated" }
544 Component.onCompleted: {
545 console.log("Name of first state:", states[0].name)
546 for (var i=0; i<states.length; i++)
547 console.log("state", i, states[i].name)
552 The defined \l State objects will be added to the \c states list
553 in the order in which they are defined.
555 If the list only contains one object, the square brackets may be omitted:
561 width: 100; height: 100
562 states: State { name: "activated" }
566 Note that objects cannot be individually added to or removed from
567 the list once created; to modify the contents of a list, it must be
568 reassigned to a new list.
570 \note The \c list type is not recommended as a type for custom properties.
571 The \c var type should be used instead for this purpose as
572 lists stored by the \c var type can be manipulated with greater
573 flexibility from within QML.
575 \sa {QML Basic Types}
580 \ingroup qmlbasictypes
581 \brief a generic property type.
583 The \c var type is a generic property type that can refer to any data type.
585 It is equivalent to a regular JavaScript variable.
586 For example, var properties can store numbers, strings, objects,
587 arrays and functions:
591 property var aNumber: 100
592 property var aBool: false
593 property var aString: "Hello world!"
594 property var anotherString: String("#FF008800")
595 property var aColor: Qt.rgba(0.2, 0.3, 0.4, 0.5)
596 property var aRect: Qt.rect(10, 10, 10, 10)
597 property var aPoint: Qt.point(10, 10)
598 property var aSize: Qt.size(10, 10)
599 property var aVector3d: Qt.vector3d(100, 100, 100)
600 property var anArray: [1, 2, 3, "four", "five", (function() { return "six"; })]
601 property var anObject: { "foo": 10, "bar": 20 }
602 property var aFunction: (function() { return "one"; })
606 It is important to note that changes in regular properties of JavaScript
607 objects assigned to a var property will \b{not} trigger updates of bindings
608 that access them. The example below will display "The car has 4 wheels" as
609 the change to the wheels property will not cause the reevaluation of the
610 binding assigned to the "text" property:
614 property var car: new Object({wheels: 4})
617 text: "The car has " + car.wheels + " wheels";
620 Component.onCompleted: {
626 If the onCompleted handler instead had \tt{"car = new Object({wheels: 6})"}
627 then the text would be updated to say "The car has 6 wheels", since the
628 car property itself would be changed, which causes a change notification
631 \section1 Using Scarce Resources with the var Type
633 A \c var type property can also hold an image or pixmap.
634 A \c var which contains a QPixmap or QImage is known as a
635 "scarce resource" and the declarative engine will attempt to
636 automatically release such resources after evaluation of any JavaScript
637 expression which requires one to be copied has completed.
639 Clients may explicitly release such a scarce resource by calling the
640 "destroy" method on the \c var property from within JavaScript. They
641 may also explicitly preserve the scarce resource by calling the
642 "preserve" method on the \c var property from within JavaScript.
643 For more information regarding the usage of a scarce resource, please
644 see \l{Scarce Resources in JavaScript}.
646 \sa {QML Basic Types}
652 \qmlbasictype variant
653 \ingroup qmlbasictypes
654 \brief a generic property type.
656 The \c variant type is a generic property type. It is obsolete and exists only to
657 support old applications; new applications should use \l var type
660 A variant type property can hold any of the \l {QML Basic Types}{basic type}
665 property variant aNumber: 100
666 property variant aString: "Hello world!"
667 property variant aBool: false
671 When integrating with C++, note that any QVariant value
672 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
673 converted into a \c variant value, and vice-versa.
676 \section1 Using Scarce Resources with the variant Type
678 A \c variant type property can also hold an image or pixmap.
679 A \c variant which contains a QPixmap or QImage is known as a
680 "scarce resource" and the declarative engine will attempt to
681 automatically release such resources after evaluation of any JavaScript
682 expression which requires one to be copied has completed.
684 Clients may explicitly release such a scarce resource by calling the
685 "destroy" method on the \c variant property from within JavaScript. They
686 may also explicitly preserve the scarce resource by calling the
687 "preserve" method on the \c variant property from within JavaScript.
688 For more information regarding the usage of a scarce resource, please
689 see \l{Scarce Resources in JavaScript}.
691 \section1 Storing Arrays and Objects
693 The \c variant type can also hold:
696 \li An array of \l {QML Basic Types}{basic type} values
697 \li A map of key-value pairs with \l {QML Basic Types}{basic-type} values
700 For example, below is an \c items array and an \c attributes map. Their
701 contents can be examined using JavaScript \c for loops. Individual array
702 values are accessible by index, and individual map values are accessible
707 property variant items: [1, 2, 3, "four", "five"]
708 property variant attributes: { 'color': 'red', 'width': 100 }
710 Component.onCompleted: {
711 for (var i=0; i<items.length; i++)
712 console.log(items[i])
714 for (var prop in attributes)
715 console.log(prop, "=", attributes[prop])
720 While this is a convenient way to store array and map-type values, you
721 must be aware that the \c items and \c attributes properties above are \e not
722 QML objects (and certainly not JavaScript object either) and the key-value
723 pairs in \c attributes are \e not QML properties. Rather, the \c items
724 property holds an array of values, and \c attributes holds a set of key-value
725 pairs. Since they are stored as a set of values, instead of as an object,
726 their contents \e cannot be modified individually:
730 property variant items: [1, 2, 3, "four", "five"]
731 property variant attributes: { 'color': 'red', 'width': 100 }
733 Component.onCompleted: {
735 console.log(items[0]) // This will still be '1'!
736 attributes.color = 'blue'
737 console.log(attributes.color) // This will still be 'red'!
742 Since it is not possible to individually add or remove items from a list or
743 object stored in a \c variant, the only way to modify its contents is to
744 reassign a new value. However, this is not efficent, as it causes the value
745 to be serialized and deserialized.
747 Additionally, since \c items and \c attributes are not QML objects, changing
748 their individual values do not trigger property change notifications. If
749 the above example had \c onNumberChanged or \c onAnimalChanged signal
750 handlers, they would not have been called. If, however, the \c items or
751 \c attributes properties themselves were reassigned to different values, then
752 such handlers would be called.
754 JavaScript programmers should also note that when a JavaScript object is
755 copied to an array or map property, the \e contents of the object (that is,
756 its key-value properties) are copied, rather than the object itself. The
757 property does not hold a reference to the original JavaScript object, and
758 extra data such as the object's JavaScript prototype chain is also lost in
761 \sa {QML Basic Types}
765 \qmlbasictype vector3d
766 \ingroup qmlbasictypes
767 \brief a value with x, y, and z attributes.
769 The \c vector3d type refers to a value with \c x, \c y, and \c z attributes.
771 To create a \c vector3d value, specify it as a "x,y,z" string:
774 Rotation { angle: 60; axis: "0,1,0" }
777 or with the \l{QML:Qt::vector3d()}{Qt.vector3d()} function:
780 Rotation { angle: 60; axis: Qt.vector3d(0, 1, 0) }
783 or as separate \c x, \c y, and \c z components:
786 Rotation { angle: 60; axis.x: 0; axis.y: 1; axis.z: 0 }
789 When integrating with C++, note that any QVector3D value
790 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
791 converted into a \c vector3d value, and vice-versa.
793 \sa {QML Basic Types}
797 \qmlbasictype enumeration
798 \ingroup qmlbasictypes
799 \brief a set of named values.
801 The \c enumeration type refers to a set of named values.
803 Each named value can be referred to as \c {<Type>.<value>}. For
804 example, the \l Text type has an \c AlignRight enumeration value:
807 Text { horizontalAlignment: Text.AlignRight }
810 (For backwards compatibility, the enumeration value may also be
811 specified as a string, e.g. "AlignRight". This form is not
812 recommended for new code.)
814 When integrating with C++, note that any enumeration value
815 \l{qtqml-cppintegration-data.html}{passed into QML from C++} is automatically
816 converted into an \c enumeration value, and vice-versa.
818 \sa {QML Basic Types}