QML 值類型

QML 支持內置和自定義值類型。

A 值類型 is one that is conceptually passed by value rather than by reference, such as an intstring . This contrasts with QML 對象類型 . 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{} 對象或 size{} 對象。

值類型可以用於引用:

  • 單個值 (如 int 引用單個數字)
  • A value that contains properties and methods (e.g. size 引用值具有 width and height 特性)
  • 一般類型 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.

可用值類型

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

由 QML 語言提供的內置值類型

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

bool

二進製 true/false 值

date

日期值

double

帶小數點的數字,以雙精度存儲

int

整數 (如:0、10 或 -20)

list

QML 對象列錶

real

帶小數點的數字

string

自由形式文本字符串

url

資源定位器

var

一般特性類型

variant

一般特性類型

void

空值類型

由 QML 模塊提供的值類型

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

For instance, the value types provided by the QtQml 模塊是:

point

具有 X 和 Y 屬性的值

rect

具有 X、Y、寬度及高度屬性的值

size

具有寬度和高度屬性的值

The value types provided by the QtQuick 模塊是:

color

ARGB 顔色值

font

Font value with the properties of QFont. The font type refers to a font value with the properties of QFont

matrix4x4

A matrix4x4 type is a 4-row and 4-column matrix

quaternion

A quaternion type has scalar, x, y, and z attributes

vector2d

A vector2d type has x and y attributes

vector3d

Value with x, y, and z attributes

vector4d

A vector4d type has x, y, z and w attributes

The Qt 全局對象提供 useful functions for manipulating values of value types for the Qt QML and Qt Quick 模塊。

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

You may define your own value types as described in 從 C++ 定義 QML 類型 . In order to use types provided by a particular QML module, clients must import that module in their QML documents.

用於值類型的特性改變行為

Some value types have properties: for example, the font 類型擁有 pixelSize , family and bold properties. Unlike properties of 對象類型 , 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:

Text {
    onFontChanged: console.log("font changed")
    Text { id: otherText }
    focus: true
    // changing any of the font attributes, or reassigning the property
    // to a different font value, will invoke the onFontChanged handler
    Keys.onDigit1Pressed: font.pixelSize += 1
    Keys.onDigit2Pressed: font.b = !font.b
    Keys.onDigit3Pressed: font = otherText.font
}
					

In contrast, properties of an 對象類型 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.

另請參閱 QML 類型係統 .