import QtQuick 2.12
import QtQuick.Window 2.12
Window {
id: root
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle {
id: rect
width: parent.width / 4
height: parent.height / 4
color: 'red'
// 这个是便捷方式等效于下面的
// NumberAnimation on width {
// id: rectAnimation
// to: root.width / 2
// duration: 1000
// running: false
// }
NumberAnimation {
id: rectAnimation
target: rect
properties: 'width'
to: root.width / 2 // 动画目标的宽度
duration: 1000 // 持续时间
running: false // 先不运行
}
MouseArea {
anchors.fill: parent
property bool flag: false
onClicked: {
console.log(rect.width)
if (!flag)
rectAnimation.to = root.width / 2
else
rectAnimation.to = root.width / 4
rectAnimation.running = true
flag = !flag
}
}
}
}
Qml NumberAnimation动画简单示例
最新推荐文章于 2025-11-05 09:24:02 发布
这篇博客展示了如何使用Qt Quick创建一个带有Rectangle元素的窗口,并通过鼠标点击实现宽度的动态缩放。通过NumberAnimation演示了动画效果的设置和控制。
1000

被折叠的 条评论
为什么被折叠?



