自从学习和使用qml以来已有一段时间。最初使用版本大约为Qt5.2.1。Qt更新速度非常快,目前最新版本已经为5.14.1。qml也不断增加新内容,因此记录一下学习和使用过程中qml与以前版本中更新的地方,便于大家学习和参考。
1自定义枚举
5.10中允许在QML中自定义枚举值,可以使用关键字enum来定义。方式如下:
qml文件名:MainItem.qml
import QtQuick 2.11
import QtQuick.Controls 2.4
Rectangle {
id: root
width: 1000
height: 800
enum MyType {Type1, Type2}
Row {
id: row1
anchors.top: parent.top
anchors.topMargin: 10
anchors.left: parent.left
anchors.leftMargin: 10
spacing: 20
Button {
width: 60
height: 40
text: "test"
onClicked: {
console.log(MainItem.MyType.Type1, MainItem.MyType.Type2);
}
}
}
}
这里我自定义了一个枚举,名字是MyType,里面有2个属性,分别是Type1,Type2。
注意:枚举类型、枚举值名字都要要以大写字母开头。
使用的话,无论是在当前QML内还是外部,格式为:QML文件名.枚举类型名.枚举值名。