QML Value Types

Description of QML value types

QML supports built-in and custom value types.

A value type is one that is conceptually passed by value rather than by reference, such as an int or a string. This contrasts with QML Object Types . Object types are passed by reference. If you assign an instance of an object type to two different properties, both properties carry the same value. Modifying the object is reflected in both properties. If you assign an instance of a value type to two different properties, the properties carry separate values. If you modify one of them, the other one stays the same. Value types are only conceptually passed by value since it must still be possible to interact with them as if they were JavaScript objects. To facilitate this, in reality they are passed as Value Type References when you access them from JavaScript code.

Unlike an object type, a value type cannot be used to declare QML objects: it is not possible, for example, to declare an int{} object or a size{} object.

Value types can be used to refer to:

  • A single value (e.g. int refers to a single number)

  • A value that contains properties and methods (e.g. size refers to a value with width and height properties)

  • The generic type var . It can hold values of any other type but is itself a value type.

When a variable or property holds a value type and it is assigned to another variable or property, then a copy of the value is made.

Available Value Types

Some value types are supported by the engine by default and do not require an import statement to be used, while others do require the client to import the module which provides them. All of the value types listed below may be used as a property type in a QML document, with the following exceptions:

  • void, which marks the absence of a value

  • list must be used in conjunction with an object or value type as element

  • enumeration cannot be used directly as the enumeration must be defined by a registered QML object type

Built-in Value Types Provided By The QML Language

The built-in value types supported natively in the QML language are listed below:

qml-int.html

a whole number, e.g. 0, 10, or -20.

qml-bool.html

a binary true/false value.

qml-real.html

a number with a decimal point.

qml-double.html

a number with a decimal point, stored in double precision.

qml-string.html

A free form text string.

qml-url.html

a resource locator.

qml-list.html

a list of QML objects.

qml-var.html

a generic property type.

qml-variant.html

a generic property type.

qml-void.html

The empty value type.

qml-enumeration.html

a named enumeration value.

qml-date.html

a date value.

Value Types Provided By QML Modules

QML modules may extend the QML language with more value types.

For instance, the value types provided by the QtQml module are:

qml-point.html

a value with x and y attributes.

qml-size.html

a value with width and height attributes.

qml-rect.html

a value with x, y, width and height attributes.

The value types provided by the QtQuick module are:

The Qt global object provides useful functions for manipulating values of value types for the Qt Qml and Qt Quick modules.

Other Qt modules will document their value types on their respective module pages.

You may define your own value types as described in Defining QML Types from C++ . In order to use types provided by a particular QML module, clients must import that module in their QML documents.

Property Change Behavior for Value Types

Some value types have properties: for example, the font type has pixelSize, family and bold properties. Unlike properties of object types , properties of value types do not provide their own property change signals. It is only possible to create a property change signal handler for the value type property itself:

Text {
    // invalid!
    onFont.pixelSizeChanged: doSomething()

    // also invalid!
    font {
        onPixelSizeChanged: doSomething()
    }

    // but this is ok
    onFontChanged: doSomething()
}

Be aware, however, that a property change signal for a value type is emitted whenever any of its attributes have changed, as well as when the property itself changes. Take the following code, for example:

In contrast, properties of an object type emit their own property change signals, and a property change signal handler for an object-type property is only invoked when the property is reassigned to a different object value.