本教程描述如何使用Qt Creator(使用版本:QT 5.2.1)執行Qt狀態(states)和轉換(transitions)。
使用示例code創建一個應用程序顯示一個QT logo,並隨着我們點擊界面上的矩形時移動顯示logo。
5.在“庫”面板下的“資源”選項中,選中state.png圖片並將它拖到畫布中
Rectangle {
id: page
width: 360
height: 360
color: "#343434"
Image {
id: icon
x: 10
y: 20
source: "state.png"
}
Rectangle {
id: topLeftRect
x: 10
y: 20
width: 64
height: 64
color: "#00000000"
radius: 6
anchors.left: parent.left
anchors.leftMargin: 10
anchors.top: parent.top
anchors.topMargin: 20
border.width: 1
border.color: "#808080"
MouseArea {
id: mouseArea1
anchors.fill: parent
onClicked:page.state = ''
}
}
}
import QtQuick 2.0
Rectangle {
id: page
width: 360
height: 360
color: "#343434"
Image {
id: icon
x: 10
y: 20
source: "state.png"
}
Rectangle {
id: topLeftRect
x: 10
y: 20
width: 64
height: 64
color: "#00000000"
radius: 6
anchors.left: parent.left
anchors.leftMargin: 10
anchors.top: parent.top
anchors.topMargin: 20
border.width: 1
border.color: "#808080"
MouseArea {
id: mouseArea1
anchors.fill: parent
onClicked:page.state = ''
}
}
Rectangle {
id: middleRightRect
x: 296
y: 148
width: 64
height: 64
color: "#00000000"
radius: 6
anchors.right: parent.right
anchors.rightMargin: 10
anchors.verticalCenter: parent.verticalCenter
MouseArea {
id: mouseArea2
anchors.fill: parent
onClicked:page.state = 'State1' //注意此處大小寫
}
border.width: 1
border.color: "#808080"
}
Rectangle {
id: bottomLeftRect
y: 10
width: 64
height: 64
color: "#00000000"
radius: 6
anchors.left: parent.left
anchors.leftMargin: 10
anchors.bottom: parent.bottom
anchors.bottomMargin: 20
MouseArea {
id: mouseArea3
anchors.fill: parent
onClicked:page.state = 'State2'
}
border.width: 1
border.color: "#808080"
}
}
import QtQuick 2.0
Rectangle {
id: page
width: 360
height: 360
color: "#343434"
Image {
id: icon
x: 10
y: 20
source: "state.png"
}
Rectangle {
id: topLeftRect
x: 10
y: 20
width: 64
height: 64
color: "#00000000"
radius: 6
anchors.left: parent.left
anchors.leftMargin: 10
anchors.top: parent.top
anchors.topMargin: 20
border.width: 1
border.color: "#808080"
MouseArea {
id: mouseArea1
anchors.fill: parent
onClicked:page.state = ''
}
}
Rectangle {
id: middleRightRect
x: 296
y: 148
width: 64
height: 64
color: "#00000000"
radius: 6
anchors.right: parent.right
anchors.rightMargin: 10
anchors.verticalCenter: parent.verticalCenter
MouseArea {
id: mouseArea2
anchors.fill: parent
onClicked:page.state = 'State1'
}
border.width: 1
border.color: "#808080"
}
Rectangle {
id: bottomLeftRect
y: 10
width: 64
height: 64
color: "#00000000"
radius: 6
anchors.left: parent.left
anchors.leftMargin: 10
anchors.bottom: parent.bottom
anchors.bottomMargin: 20
MouseArea {
id: mouseArea3
anchors.fill: parent
onClicked:page.state = 'State2'
}
border.width: 1
border.color: "#808080"
}
states: [
State {
name: "State1"
PropertyChanges {
target: icon
x:middleRightRect.x
y:middleRightRect.y
}
},
State {
name: "State2"
PropertyChanges {
target: icon
x:bottomLeftRect.x
y:bottomLeftRect.y
}
}
]
transitions: [
Transition {
from: "*"
to: "State1"
NumberAnimation{
easing.type: Easing.OutBounce
// easing.type: Easing.InBounce //出發時的效果
properties: "x,y";
duration:1000
//進入狀態1的動畫設置(動畫持續時間1秒、進入時含彈跳效果)
}
},
Transition {
from: "*"
to: "State2"
NumberAnimation{
properties: "x,y";
easing.type:Easing.InOutQuad
// easing.type:Easing.InOutBounce //出發進入時的動畫效果
duration: 2000
//進入狀態2的動畫設置(動畫持續時間2秒)
}
},
Transition {
NumberAnimation{
properties: "x,y";
duration: 200
//進入第一個矩形的動畫效果(動畫持續時間200毫秒)
}
}
]
}