Item 0:编码规范
本节提供有关如何格式化属性,信号和函数的顺序,以使事情变得轻松并快速切换到相关代码块。
QML对象属性始终按以下顺序构造:
- id
- 属性声明
- 信号声明
- 对象声明
- 状态(States)
- 变换(Transitions)
- 信号处理器(Signal handlers)
- 子对象
- 可视对象
- Qt提供的不可见对象
- 自定义的不可见对象
- QtObject所包装的私有数据
- JavaScript函数
Rectangle {
id: photo
property bool thumbnail: false // 属性声明
property alias image: photoImage.source
signal clicked // 信号声明
x: 20
y: 20
height: 150
color: "gray" //对象声明
width: { // 较大的绑定
if (photoImage.width > 200) {
photoImage.width;
}
else {
200;
}
}
states: State { // 状态
name: "selected"
PropertyChanges { target: border; color: "red" }
}
transitions: Transition { // 变换
from: ""; to: "selected"
ColorAnimation { target: border; duration: 200 }
}
onSomeEvent: {
}
Rectangle { // 子对象 - 可视对象
id: border
anchors.centerIn: parent; color: "white"
Image { id: photoImage; anchors.centerIn: parent }
}
Timer { } // 子对象 - Qt提供的不可见对象
MyCppObject { } // 子对象 - 自定义的不可见对象
QtObject {
id: priv