QML Object Attributes▲
Every QML object type has a defined set of attributes. Each instance of an object type is created with the set of attributes that have been defined for that object type. There are several different kinds of attributes which can be specified, which are described below.
Attributes in Object Declarations▲
An object declaration in a QML document defines a new type. It also declares an object hierarchy that will be instantiated should an instance of that newly defined type be created.
The set of QML object-type attribute types is as follows:
-
the id attribute
-
property attributes
-
signal attributes
-
signal handler attributes
-
method attributes
-
attached properties and attached signal handler attributes
-
enumeration attributes
These attributes are discussed in detail below.
The id Attribute▲
Every QML object type has exactly one id attribute. This attribute is provided by the language itself, and cannot be redefined or overridden by any QML object type.
A value may be assigned to the id attribute of an object instance to allow that object to be identified and referred to by other objects. This id must begin with a lower-case letter or an underscore, and cannot contain characters other than letters, numbers and underscores.
Below is a TextInput object and a Text object. The TextInput object's id value is set to "myTextInput". The Text object sets its text property to have the same value as the text property of the TextInput, by referring to myTextInput.text. Now, both items will display the same text:
import
QtQuick 2.0
Column
{
width
:
200
; height
:
200
TextInput
{
id
:
myTextInput; text
:
"Hello World"
}
Text
{
text
:
myTextInput.text }
}
An object can be referred to by its id from anywhere within the component scope in which it is declared. Therefore, an id value must always be unique within its component scope. See Scope and Naming Resolution for more information.
Once an object instance is created, the value of its id attribute cannot be changed. While it may look like an ordinary property, the id attribute is not an ordinary property attribute, and special semantics apply to it; for example, it is not possible to access myTextInput.id in the above example.
Property Attributes▲
A property is an attribute of an object that can be assigned a static value or bound to a dynamic expression. A property's value can be read by other objects. Generally it can also be modified by another object, unless a particular QML type has explicitly disallowed this for a specific property.
Defining Property Attributes▲
A property may be defined for a type in C++ by registering a Q_PROPERTY of a class which is then registered with the QML type system. Alternatively, a custom property of an object type may be defined in an object declaration in a QML document with the following syntax:
[default
] [required] [readonly] property &
lt;propertyType&
gt; &
lt;propertyName&
gt;
In this way an object declaration may expose a particular value to outside objects or maintain some internal state more easily.
Property names must begin with a lower case letter and can only contain letters, numbers and underscores. JavaScript reserved words are not valid property names. The default, required, and readonly keywords are optional, and modify the semantics of the property being declared. See the upcoming sections on default properties, required properties and, read-only properties for more information about their respective meaning.
Declaring a custom property implicitly creates a value-change signal for that property, as well as an associated signal handler called on<PropertyName>Changed, where <PropertyName> is the name of the property, with the first letter capitalized.
For example, the following object declaration defines a new type which derives from the Rectangle base type. It has two new properties, with a signal handler implemented for one of those new properties:
Rectangle
{
property
color
previousColor
property
color
nextColor
onNextColorChanged
:
console.log("The next color will be: "
+
nextColor.toString())
}
Valid Types in Custom Property Definitions▲
Any of the QML Basic Types aside from the enumeration type can be used as custom property types. For example, these are all valid property declarations:
Item
{
property
int
someNumber
property
string
someString
property
url
someUrl
}
(Enumeration values are simply whole number values and can be referred to with the int type instead.)
Some basic types are provided by the QtQuick module and thus cannot be used as property types unless the module is imported. See the QML Basic Types documentation for more details.
Note the var basic type is a generic placeholder type that can hold any type of value, including lists and objects:
property var someNumber: 1.5
property var someString: "abc"
property var someBool: true
property var someList: [1
, 2
, "three"
, "four"
]
property var someObject: Rectangle {
width: 100
; height: 100
; color: "red"
}
Additionally, any QML object type can be used as a property type. For example:
property Item someItem
property Rectangle someRectangle
This applies to custom QML types as well. If a QML type was defined in a file named ColorfulButton.qml (in a directory which was then imported by the client), then a property of type ColorfulButton would also be valid.
Assigning Values to Property Attributes▲
The value of a property of an object instance may be specified in two separate ways:
-
a value assignment on initialization
-
an imperative value assignment
In either case, the value may be either a static value or a binding expression value.
Value Assignment on Initialization▲
The syntax for assigning a value to a property on initialization is:
&
lt;propertyName&
gt; : &
lt;value&
gt;
An initialization value assignment may be combined with a property definition in an object declaration, if desired. In that case, the syntax of the property definition becomes:
[default
] property &
lt;propertyType&
gt; &
lt;propertyName&
gt; : &
lt;value&
gt;
An example of property value initialization follows:
import
QtQuick 2.0
Rectangle
{
color
:
"red"
property
color
nextColor
:
"blue"
// combined property declaration and initialization
}
Imperative Value Assignment▲
An imperative value assignment is where a property value (either static value or binding expression) is assigned to a property from imperative JavaScript code. The syntax of an imperative value assignment is just the JavaScript assignment operator, as shown below:
[&
lt;objectId&
gt;.]&
lt;propertyName&
gt; =
value
An example of imperative value assignment follows:
import
QtQuick 2.0
Rectangle
{
id
:
rect
Component.onCompleted
: {
rect.
color =
"red"
}
}
Static Values and Binding Expression Values▲
As previously noted, there are two kinds of values which may be assigned to a property: static values, and binding expression values. The latter are also known as property bindings.
Kind |
Semantics |
---|---|
Static Value |
A constant value which does not depend on other properties. |
Binding Expression |
A JavaScript expression which describes a property's relationship with other properties. The variables in this expression are called the property's dependencies. The QML engine enforces the relationship between a property and its dependencies. When any of the dependencies change in value, the QML engine automatically re-evaluates the binding expression and assigns the new result to the property. |
Here is an example that shows both kinds of values being assigned to properties:
import
QtQuick 2.0
Rectangle
{
// both of these are static value assignments on initialization
width
:
400
height
:
200
Rectangle
{
// both of these are binding expression value assignments on initialization
width
:
parent.width /
2
height
:
parent.height
}
}
To assign a binding expression imperatively, the binding expression must be contained in a function that is passed into Qt.binding(), and then the value returned by Qt.binding() must be assigned to the property. In contrast, Qt.binding() must not be used when assigning a binding expression upon initialization. See Property Binding for more information.
Type Safety▲
Properties are type safe. A property can only be assigned a value that matches the property type.
For example, if a property is a real, and if you try to assign a string to it, you will get an error:
property int
volume: "four"
// generates an error; the property's object will not be loaded
Likewise if a property is assigned a value of the wrong type during run time, the new value will not be assigned, and an error will be generated.
Some property types do not have a natural value representation, and for those property types the QML engine automatically performs string-to-typed-value conversion. So, for example, even though properties of the color type store colors and not strings, you are able to assign the string "red" to a color property, without an error being reported.
See QML Basic Types for a list of the types of properties that are supported by default. Additionally, any available QML object type may also be used as a property type.
Special Property Types▲
Object List Property Attributes▲
A list type property can be assigned a list of QML object-type values. The syntax for defining an object list value is a comma-separated list surrounded by square brackets:
[ &
lt;item 1
&
gt;, &
lt;item 2
&
gt;, ... ]
For example, the Item type has a states property that is used to hold a list of State type objects. The code below initializes the value of this property to a list of three State objects:
import
QtQuick 2.0
Item
{
states
:
[
State
{
name
:
"loading"
}
,
State
{
name
:
"running"
}
,
State
{
name
:
"stopped"
}
]
}
If the list contains a single item, the square brackets may be omitted:
import
QtQuick 2.0
Item
{
states
:
State
{
name
:
"running"
}
}
A list type property may be specified in an object declaration with the following syntax:
[default
] property list&
lt;&
lt;objectType&
gt;&
gt; propertyName
and, like other property declarations, a property initialization may be combined with the property declaration with the following syntax:
[default
] property list&
lt;&
lt;objectType&
gt;&
gt; propertyName: &
lt;value&
gt;
An example of list property declaration follows:
import
QtQuick 2.0
Rectangle
{
// declaration without initialization
property
list
&
lt;Rectangle
&
gt; siblingRects
// declaration with initialization
property
list
&
lt;Rectangle
&
gt; childRects
:
[
Rectangle
{
color
:
"red"
}
,
Rectangle
{
color
:
"blue"
}
]
}
If you wish to declare a property to store a list of values which are not necessarily QML object-type values, you should declare a var property instead.
Grouped Properties▲
In some cases properties contain a logical group of sub-property attributes. These sub-property attributes can be assigned to using either the dot notation or group notation.
For example, the Text type has a font group property. Below, the first Text object initializes its font values using dot notation, while the second uses group notation:
Text {
//dot notation
font.pixelSize: 12
font.b: true
}
Text {
//group notation
font {
pixelSize: 12
; b: true
}
}
Grouped property types are basic types which have subproperties. Some of these basic types are provided by the QML language, while others may only be used if the Qt Quick module is imported. See the documentation about QML Basic Types for more information.
Property Aliases▲
Property aliases are properties which hold a reference to another property. Unlike an ordinary property definition, which allocates a new, unique storage space for the property, a property alias connects the newly declared property (called the aliasing property) as a direct reference to an existing property (the aliased property).
A property alias declaration looks like an ordinary property definition, except that it requires the alias keyword instead of a property type, and the right-hand-side of the property declaration must be a valid alias reference:
[default
] property alias &
lt;name&
gt;: &
lt;alias reference&
gt;
Unlike an ordinary property, an alias has the following restrictions:
-
It can only refer to an object, or the property of an object, that is within the scope of the type within which the alias is declared.
-
It cannot contain arbitrary JavaScript expressions
-
It cannot refer to objects declared outside of the scope of its type.
-
The alias reference is not optional, unlike the optional default value for an ordinary property; the alias reference must be provided when the alias is first declared.
-
It cannot refer to attached properties.
-
It cannot refer to properties inside a hierarchy with depth 3 or greater. The following code will not work:
Sélectionnezproperty alias color: myItem.myRect.border.color Item
{
id
:
myItem property Rectangle myRect}
However, aliases to properties that are up to two levels deep will work.
Sélectionnezproperty alias color: rectangle.border.color Rectangle
{
id
:
rectangle}
For example, below is a Button type with a buttonText aliased property which is connected to the text object of the Text child: