自從學習和使用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文件名.枚舉類型名.枚舉值名。