Qt属性动画之QPropertyAnimation

https://github.com/mygit03/MyAnimation.git

#include "myanimation.h"

#include <QLabel>
#include <QPushButton>
#include <QPropertyAnimation>

MyAnimation::MyAnimation(QWidget *parent)
    : QWidget(parent)
{
    resize(500, 300);

#if 0
    /*  声明动画类,并将控制对象 this (this一定是继承自QObject的窗口部件)  以及属性名 "geometry" 传入构造函数  */
    QPropertyAnimation* animation = new QPropertyAnimation(this, "geometry");
    /*  设置动画持续时长为 2 秒钟  */
    animation->setDuration(2000);
    /*  设置动画的起始状态 起始点 (1,2)  起始大小 (3,4)  */
    animation->setStartValue(QRect(1, 2, 300, 400));
    /*  设置动画的结束状态 结束点 (100,200)  结束大小 (300,400)  */
    animation->setEndValue(QRect(100, 200, 300, 400));
    /*  设置动画效果  */
    animation->setEasingCurve(QEasingCurve::CosineCurve);
    /*  开始执行动画 QAbstractAnimation::DeleteWhenStopped 动画结束后进行自清理(效果就好像智能指针里的自动delete animation) */
    animation->start(QAbstractAnimation::DeleteWhenStopped);
#endif

    QPushButton *btn_start = new QPushButton(tr("开始"), this);
    btn_start->setGeometry(10,10,50,25);
    QPushButton *btn_stop = new QPushButton(tr("停止"), this);
    btn_stop->setGeometry(65,10,50,25);
    QLabel *label = new QLabel(tr("<font color='red'>hello world!</font>"), this);
    label->setGeometry(200, 130, 80, 20);
    label->setMinimumWidth(80);

    QPropertyAnimation* animation = new QPropertyAnimation(label, "geometry");
    animation->setDuration(2000);
    animation->setStartValue(QRect(50, 30, 30, 20));
    animation->setEndValue(QRect(300, 270, 30, 20));
    animation->setEasingCurve(QEasingCurve::CosineCurve);
    animation->setLoopCount(-1);        //一直运行,直到stop

    connect(btn_start, SIGNAL(clicked(bool)), animation, SLOT(start()));
    connect(btn_stop, SIGNAL(clicked(bool)), animation, SLOT(stop()));
}

MyAnimation::~MyAnimation()
{

}

参考教程:

QPropertyAnimation 几行代码快速制作流畅的动画效果

【qt】动画按钮---QPropertyAnimation的使用

Qt之QPropertyAnimation








  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值