QML类型系统
在QML文档中的对象层次结构定义中可以使用的类型可以来自各种来源。它们可能是:
- 由QML语言本地提供
- QML模块通过C ++注册
- 由QML模块作为QML文档提供
基本类型
基本型是一种指的是简单的值,例如一个int或一个string。这与QML对象类型相反,QML对象是指具有属性,信号,方法等的对象。基本类型与对象类型不同,不能用于声明QML对象:例如,不能声明int{}对象或size{}对象。
- 单个值(例如,int表示单个数字,var表示单个项目列表)
- 包含一组简单的名-值对的值(例如,size是指具有
width和height属性的值)
支持的基本类型
引擎默认支持某些基本类型,不需要使用import语句,而其他一些则需要客户端导入提供它们的模块。下面列出的所有基本类型都可以用作QML文档中的类型,但以下情况除外:
list必须与QML对象类型结合使用enumeration不能直接使用,因为枚举必须由已注册的QML对象类型定义
QML语言提供的基本类型
以下列出了QML语言本身支持的基本类型:
bool |
Binary true/false value |
double |
Number with a decimal point, stored in double precision |
enumeration |
Named enumeration value |
int |
Whole number, e.g. 0, 10, or -20 |
list |
List of QML objects |
real |
Number with a decimal point |
string |
Free form text string |
url |
Resource locator |
var |
Generic property type |
QML模块提供的基本类型
QML模块可以使用更多基本类型扩展QML语言。例如,QtQuick下面列出了模块提供的基本类型:
date |
Date value |
point |
Value with x and y attributes |
rect |
Value with x, y, width and height attributes |
size |
Value with width and height attributes |
基本类型的属性更改行为
一些基本类型具有属性:例如,字体类型具有pixelSize,family和bold特性。与对象类型的属性不同,基本类型的属性不提供自己的属性更改信号。只能为基本类型属性本身创建属性更改信号处理程序:
Text {// invalid!onFont.pixelSizeChanged: doSomething()// also invalid!font {onPixelSizeChanged: doSomething()}// but this is okonFontChanged: doSomething()
}//属性中的基本类型发生改变,会调用 属性改变操作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 handlerKeys.onDigit1Pressed: font.pixelSize += 1Keys.onDigit2Pressed: font.b = !font.bKeys.onDigit3Pressed: font = otherText.font
}