Component是可重用的、封装的、具有定义良好的接口的QML类型。组件通常由组件文件定义,即.qml文件。组件类型本质上允许在QML文件中内联定义QML组件,而不是作为单独的QML文件。这对于在QML文件中重用小组件或定义逻辑上属于文件中其它QML组件的组件可能很有用。
1. 在单独文件中定义Component,文件名即是组件名称,且首字母必须为大写,例如创建BusyIndicator.qml文件,样式可以随意写,顶层Item可使用Rectangle,control,Button,Window等等...
import QtQuick 2.0
import QtQuick.Controls 2.2
Rectangle {
id: busyIndicatorComponents
property bool running: false
signal indicatorClicked
width: 20
height: 20
Button {
id: indicatorBtn
anchors.fill: parent
background: Image {
id: indicatorImage
source: indicatorBtn.pressed ? "../images/h.png" : "../images/n.png"
}
onClicked: {
indicatorClicked() //点击组件可发出信号
}
NumberAnimation on rotation {
id: rotaionAnimation