首页 > 其他分享 >Qt动画

Qt动画

时间:2023-02-08 13:11:43浏览次数:34  
标签:动画 widget Qt QPropertyAnimation QRect animation QEasingCurve

一、介绍

类名 功能介绍
QAbstractAnimation 动画基类 提供基本的动画属性和接口,它有两个子类QVariantAnimation 和QAnimationGroup。 QAbstractAnimation是其他所有类的父类。 它提供了基础的属性,适用于所有的本框架下的动画。
 QPropertyAnimation 实际的动画类 实现了一个Qt动画属性,比如对控件的大小缩放、位置移动、透明度变化的动画效果实现。 修改的属性一定是类具有的属性, 类中要有属性定义Q_PROPERTY(QRect geometry READ geometry WRITE setGeometry), 否则要声明属性,并实践READ和WRITE方法。
QParallelAnimationGroup 并行动画类  将多个属性动画QPropertyAnimation添加到一个QParallelAnimationGroup, 实现并行执行动画。
QSequentialAnimationGroup 串行动画类 QSequentialAnimationGroup, 将多个QPropertyAnimation串联在一起实现, 按照添加顺序先后执行动画。
QPauseAnimation 停顿类 在串行动画中,添加一个暂停的动画,可以实现延时效果。
QEasingCurve 速度曲线类 Qt动画运动的速度曲线,枚举了45种

(一)QPropertyAnimation 

QPropertyAnimation 对 Qt 属性进行插值。由于属性值存储在 QVariant 中,该类继承了 QVariantAnimation,并支持与其超类相同元类型的动画。

1.常用接口函数

setTargetObject:设置仿真对象
setPropertyName:设置仿真属性的名称,
setDuration:设置仿真持续的时间
setStartValue:设置初始值
setEndValue:设置结束值
start:开始仿真
currentValue:返回当前值
setKeyValueAt:设置关键点的值
valueChanged:只要仿真追踪的值发生变化,就发送该信号

  • void setDuration(int msecs)函数是设置动画持续的时间。
  • voidsetEasingCurve(const QEasingCurve & easing)函数可以设置动画的缓和曲线,可以理解为移动曲线。例如QEasingCurve::InCirc可以提供圆形凹型曲线,QEasingCurve::Linear可以提供一个直线。
    • QEasingCurve::InBounce
    • QEasingCurve::OutBounce
    • QEasingCurve::InOutBounce
    • QEasingCurve::OutInBounce
    • QEasingCurve::InElastic
    • QEasingCurve::OutElastic
    • QEasingCurve::InOutElastic
    • QEasingCurve::OutInElastic
  • voidsetStartValue(const QVariant & value)用于设置动画的开始位置,这个位置和属性是密切相关的,属性的Type是什么,这里的value也必须是什么Type的,比如在propertyName为geometry,其属性是QRect,这里的数据的Type也应该是QRect.
  • voidsetEndValue(const QVariant & value)和voidsetStartValue(const QVariant & value)一致,设置的是动画结束的位置。

(二)QSequentialAnimationGroup

该类就是用来按照动画添加顺序来执行动画的。我们只用实例化该类,然后通过调用addAnimation()或者insertAnimation()方法把各个动画添加进去就可以了

  • QSequentialAnimationGroup:串行动画组,按照添加的先后顺序,动画依次执行。
  • QParallelAnimationGroup:并行动画组,不区分先后顺序,动画同时执行。

二、样例

(一)按键动画效果

1.分步下降上升

void MyPushButton::zoom1()
{
    //创建动态对象
    QPropertyAnimation * animation = new QPropertyAnimation(this,"geometry");
    //设置动画时间间隔
    animation->setDuration(200);

    //起始位置
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));

    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);

    //开始执行动画
    animation->start();
}

void MyPushButton::zoom2()
{
    //创建动态对象
    QPropertyAnimation * animation = new QPropertyAnimation(this,"geometry");

    //设置动画时间间隔
    animation->setDuration(200);

    //起始位置
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));

    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);

    //开始执行动画
    animation->start();

}
//使用
connect(startBtn,&MyPushButton::clicked,[=](){
        //播放开始音效资源
        startSound->play();
        startBtn->zoom1(); //向下跳跃
        startBtn->zoom2(); //向上跳跃

        //延时进入到选择关卡场景中,好显示出按键动画效果
        QTimer::singleShot(500,this,[=](){
            //设置chooseScene场景的位置
            chooseScene->setGeometry(this->geometry());
            //自身隐藏
            this->hide();
            //显示选择关卡场景
            chooseScene->show();
        });
});

2.顺序执行

// 弹跳动画
void zoom(QWidget* widget)
{
    int x = widget->x();
    int y = widget->y();

    // 用QSequentialAnimationGroup串联下降和上升两个动画
    QSequentialAnimationGroup* group = new QSequentialAnimationGroup;
    
    // 下降动画
    QPropertyAnimation* animation = new QPropertyAnimation(widget, "geometry");

    animation->setDuration(ZOOM_DURATION);
    animation->setStartValue(QRect(x, y, widget->width(), widget->height()));
    animation->setEndValue(QRect(x, y + 10, widget->width(), widget->height()));
    animation->setEasingCurve(QEasingCurve::OutBounce);

    group->addAnimation(animation);

    // 上升动画
    animation->setStartValue(QRect(x, y + 10, widget->width(), widget->height()));
    animation->setEndValue(QRect(x, y, widget->width(), widget->height()));
    
    group->addAnimation(animation);

    group->start();
   
}
//使用
connect(m_button[0], &ClickLabel::clicked, this, [=]() {
    zoom(m_button[0]);
    QTimer::singleShot(ZOOM_DURATION*1.5, this, [=]() {
        m_guideScene->show();
        this->hide();
        });
    });

 

标签:动画,widget,Qt,QPropertyAnimation,QRect,animation,QEasingCurve
From: https://www.cnblogs.com/imreW/p/17101355.html

相关文章

  • QT使用画家设置背景图片
    protected:voidpaintEvent(QPaintEvent*);voidPet::paintEvent(QPaintEvent*){//重写自动执行QPixmappixmap=QPixmap("./images/background.jpg").......
  • 【Qt】-学Qt前的准备
    文章目录​​1Qt概述​​​​1.1什么是Qt​​​​2创建第一个Qt程序​​​​3命名规范以及快捷键​​​​3.1.pro文件介绍​​​​3.2.h文件介绍​​​​3.3命名规范......
  • Qt QLabel设置字体大小
    方法一(使用ui布局):选中label控件搜索“font” 修改pointsize后面的值方法二(使用代码):设置字体大小使用QFont的setPontSize1QLabel*lb=newQLabel(tr("examp......
  • Qt QLabel设置字体、大小、加粗等
    QFontfont(“MicrosoftYaHei”,10,75);//第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75)ui->label->setFont(font);常见权重QFont::Light-25高亮QF......
  • PyQt 主窗口、子窗口交互
    主窗口main.pyfromPySide6.QtCoreimport(QCoreApplication,QDate,QDateTime,QLocale,QMetaObject,QObject,QPoint,QRect,QSize,QTime,QUrl,Qt)fr......
  • 【嵌入式】微芯旺KungFu32A156MQT使用PWM实现呼吸灯
    由于例程给我的IO口是G8,但是我的板子上没有暴露G8引脚,所以需要查看数据手册,重映射一个IO口作为CCP的PWM输出 例:可见PB10使用AF2重映射到CCP0的通道3上,所以使用PB10观......
  • STM32MP157开发板Linux+Qt项目实战:智慧家庭
    stm32mp157开发板FS-MP1A是华清远见自主研发的一款高品质、高性价比的Linux+单片机二合一的嵌入式教学级开发板。开发板搭载ST的STM32MP157高性能微处理器,集成2个Cortex-A7......
  • 整合MQTT
    1、步骤(1)dependencecom.google.code.gsongsonorg.springframework.integrationspring-integration-streamorg.springframework.integrationspring-integration......
  • Qt::WA_TransparentForMouseEvents
    (一)Qt::WA_TransparentForMouseEvents实现鼠标穿透功能,类似“隔空取物、隔山打牛”的效果。//qwidget.hvoidsetAttribute(Qt::WidgetAttribute,boolon=true);启......
  • QT中的类
    1.QChar表示一个字符的类,包含于QtCore判断:boolisDigit()const;//判断是否是十进制数字('0'-'9')boolisLetter()const;//判断是否是字母boolisNumber()const;//判......