QML 支持許多基本類型。
A
基本類型
是某種簡單值的引用,譬如
int
或
string
。這相比
QML 對象類型
, which refers to an object with properties, signals, methods and so on. Unlike an object type, a basic type cannot be used to declare QML objects: it is not possible, for example, to declare an
int{}
對象或
size{}
對象。
Basic types can be used to refer to:
width
and
height
屬性)
Some basic 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 basic types listed below may be used as a
property
type in a QML document, with the following exceptions:
list
must be used in conjunction with a QML object type
enumeration
cannot be used directly as the enumeration must be defined by a registered QML object type
The basic types supported natively in the QML language are listed below:
| bool | 二進製 true/false 值 |
| double | 帶小數點的數字,以雙精度存儲 |
| enumeration | 命名枚舉值 |
| int | 整數 (如:0、10 或 -20) |
| list | QML 對象列錶 |
| real | 帶小數點的數字 |
| string | 自由形式文本字符串 |
| url | 資源定位器 |
| var | 一般特性類型 |
QML modules may extend the QML language with more basic types. For example, the basic types provided by the
QtQuick
module are listed below:
| date | 日期值 |
| point | 具有 X 和 Y 屬性的值 |
| rect | 具有 X、Y、寬度及高度屬性的值 |
| size | 具有寬度和高度屬性的值 |
| color | ARGB color value. The color type refers to an ARGB color value. It can be specified in a number of ways: |
| 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 global object provides useful functions for manipulating values of basic types.
Currently only QML modules which are provided by Qt may provide their own basic types, however this may change in future releases of Qt QML. In order to use types provided by a particular QML module, clients must import that module in their QML documents.
一些基本類型擁有特性:例如,
font
類型擁有
pixelSize
,
family
and
bold
properties. Unlike properties of
對象類型
, properties of basic types do not provide their own property change signals. It is only possible to create a property change signal handler for the basic 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 basic 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 類型係統 .