通过states属性,立马切换相关属性的值
如下:
鼠标进入时,颜色变绿,宽度变宽
鼠标按下时,颜色变蓝
鼠标释放时,颜色和宽度回到最开始的样子
代码如下:
import QtQuick
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle
{
id:rect1
width: 300
height: 300
color:"red"
//指定一个初始状态
state: "normal"
//states属性可以定义多个状态,是一个数组
states:[
//State是一个元素
State {
name: "noraml"
//定义该状态下的相关属性的值
PropertyChanges {
//指定是哪个元素
target: rect1
color:"red"
width:300
}
//还可以继续追加PropertyChanges,里面的taget也可以是其他元素
},
State {
name: "green_color"
PropertyChanges {
target: rect1
color:"green"
width:400
}
},
State {
name: "blue_color"
PropertyChanges {
target: rect1
color:"blue"
width:400
}
}
]
//找机会切换状态,这里举例:鼠标相关操作时切换状态
MouseArea{
anchors.fill: parent
hoverEnabled: true
onEntered: {
//鼠标进入时,状态切换
rect1.state="green_color"
}
onPressed: {
//鼠标按下时,状态切换
rect1.state="blue_color"
}
onReleased: {
//鼠标释放时,状态切换到最开始的状态
rect1.state="normal"
}
onExited: {
//鼠标离开时,状态切换到最开始的状态
rect1.state="normal"
}
}
}
}
通过transitions属性来给状态切换加动画,而不是瞬间切换
颜色和宽度的切换都有1s的缓冲
代码如下:
import QtQuick
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle
{
id:rect1
width: 300
height: 300
color:"red"
//指定一个初始状态
state: "normal"
//states属性可以定义多个状态,是一个数组
states:[
//State是一个元素
State {
name: "noraml"
//定义该状态下的相关属性的值
PropertyChanges {
//指定是哪个元素
target: rect1
color:"red"
width:300
}
//还可以继续追加PropertyChanges,里面的taget也可以是其他元素
},
State {
name: "green_color"
PropertyChanges {
target: rect1
color:"green"
width:400
}
},
State {
name: "blue_color"
PropertyChanges {
target: rect1
color:"blue"
width:400
}
}
]
transitions: [
Transition {
//从normal状态到green_color状态
from: "normal"
to: "green_color"
//颜色变化了,时长为1s
ColorAnimation {
duration: 1000
}
//宽度变化了,时长为1s
//使用NumberAnimation或者PropertyAnimation元素都行
NumberAnimation{
property: "width"
duration: 1000
}
},
Transition {
//从green_color状态到blue_color状态
from: "green_color"
to: "blue_color"
//颜色变化了,时长为1s
ColorAnimation {
duration: 1000
}
//宽度变化了,时长为1s
NumberAnimation{
property: "width"
duration: 1000
}
},
Transition {
//从blue_color状态到normal状态
from: "blue_color"
to: "normal"
//颜色变化了,时长为1s
ColorAnimation {
duration: 1000
}
//宽度变化了,时长为1s
NumberAnimation{
property: "width"
duration: 1000
}
}
]
MouseArea{
anchors.fill: parent
hoverEnabled: true
onEntered: {
//鼠标进入时,状态切换
rect1.state="green_color"
}
onPressed: {
//鼠标按下时,状态切换
rect1.state="blue_color"
}
onReleased: {
//鼠标释放时,状态切换到最开始的状态
rect1.state="normal"
}
onExited: {
//鼠标离开时,状态切换到最开始的状态
rect1.state="normal"
}
}
}
}
通过元素PropertyAnimation和NumberAnimation预先定义动画
点击矩形时颜色变化,透明度变化,宽度变化
代码如下:
import QtQuick
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle
{
id:rect1
width: 300
height: 300
color:"red"
opacity: 1
//预先定义好动画效果
PropertyAnimation{
id:color_change
//rect1的颜色变为绿色,时间1s
target: rect1
properties: "color"
to:"green"
duration:1000
}
NumberAnimation{
id:opacity_change
//rect1透明度渐变,时间1s
target: rect1
properties: "opacity"
from:0.1
to:1
duration:1000
}
NumberAnimation{
id:width_change
//rect1宽度变宽,时间1s
target: rect1
properties: "width"
to:400
duration:1000
}
MouseArea{
anchors.fill: parent
onClicked: {
//点击的时候启动预先定义好的动画
color_change.start();
opacity_change.start();
width_change.start();
}
}
}
}
通过PropertyAnimation/NumberAnimation/ColorAnimation on 某某属性:元素显示时立马产生动画效果
宽度变宽,颜色变黄,变得不透明
代码如下:
import QtQuick
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle
{
id:rect1
width: 300
height: 300
color:"red"
opacity: 1
//矩形刚显示时,颜色就逐渐变成黄色
ColorAnimation on color{
to: "yellow"
duration: 1000
}
//宽度逐渐边长
PropertyAnimation on width {
from:0
to:400
duration: 1000
}
//逐渐从透明变为不透明
NumberAnimation on opacity {
from: 0
to:1
duration: 1000
}
}
}
还可以通过SequentialAnimation指定多个动画的执行顺序
先沿x方向移动,同时宽度变宽
然后沿着沿方向移动,同时变透明
代码如下:
import QtQuick
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle {
id: rect
width: 100; height: 100
color: "red"
//创建多个动画,然后按照书写顺序执行
//立马执行:on 某某属性
SequentialAnimation on x{
//running: true
//先沿x方向移动
NumberAnimation {
target: rect;
property: "x";
to: 50;
duration: 1000
}
//再沿y方向移动
NumberAnimation {
target: rect;
property: "y";
to: 50;
duration: 1000
}
}
//多个SequentialAnimation整体是同时执行的,但SequentialAnimation里面的是按顺序执行的
SequentialAnimation{
//running置为true也可以立马执行
running: true
id:change
//宽度先变宽
NumberAnimation {
target: rect;
property: "width";
to: 200;
duration: 1000
}
//然后变透明
NumberAnimation {
target: rect;
property: "opacity";
to: 0;
duration: 1000
}
}
}
}
还可以通过Behavior元素来绑定属性,属性变化时加动画
鼠标进入颜色变绿,鼠标离开颜色变红
代码如下:
import QtQuick
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle {
width: 400
height: 400
Rectangle {
id: coloredRect
width: 100
height: 100
anchors.centerIn: parent
//搭配states属性
//绑定到颜色,颜色变化时,加上变化时长
Behavior on color {
ColorAnimation {
duration: 1000
}
}
MouseArea {
id: mouser
anchors.fill: parent
hoverEnabled: true
}
states: [
State {
name: "GreenState"
//鼠标进入时颜色变绿
when: mouser.containsMouse
PropertyChanges {
target: coloredRect
color: "green"
}
},
State {
name: "RedState"
//鼠标离开时颜色变红
when: !mouser.containsMouse
PropertyChanges {
target: coloredRect
color: "red"
}
}]
}
}
}