首页 > 其他分享 >Qt 动画之二:简单实例:

Qt 动画之二:简单实例:

时间:2023-02-10 17:23:52浏览次数:38  
标签:pAnimation 动画 Qt distance 之二 QWidget rect target

一、效果

Qt_Animation.gif

二、代码

Widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QPropertyAnimation>
#include <QDebug>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);

protected:
    void showEvent(QShowEvent *event);

private:
    QPushButton *m_pBtnRed;
    QPushButton *m_pBtnBlue;
    QPushButton *m_pBtnYellow;

    QWidget *m_pWidgetRed;
    QWidget *m_pWidgetBlue;
    QWidget *m_pWidgetYellow;
};
#endif // WIDGET_H

Widget.cpp

#include "Widget.h"

// 透明度改变动画(windowOpacity属性只能对顶级窗口使用,例如Qt::Tool窗口,对普通控件这些无效)
template<typename func>
static inline void animationOpacity(QWidget *target, const QVariant &startValue, const QVariant &endValue, int duration, func onFinished = nullptr)
{
    QPropertyAnimation *pAnimation = new QPropertyAnimation(target, "windowOpacity", target);
    pAnimation->setStartValue(target->property("windowOpacity"));
    pAnimation->setStartValue(startValue);
    pAnimation->setEndValue(endValue);
    pAnimation->setDuration(duration);
    pAnimation->start(QAbstractAnimation::DeleteWhenStopped); // 动画完成后自动删除
    QObject::connect(pAnimation, &QPropertyAnimation::finished, target, onFinished);
}

// 摇晃动画
template<typename func>
static inline void animationShake(QWidget *target, int duration, int distance, func onFinished = nullptr)
{
    QRect rect = target->rect().adjusted(target->x(), target->y(), 0, 0);

    QPropertyAnimation *pAnimation = new QPropertyAnimation(target, "geometry");
    pAnimation->setDuration(duration);
    pAnimation->setKeyValueAt(0, rect.adjusted(-distance, distance, 0, 0)); // 设置动画结束位置及其大小
    pAnimation->setKeyValueAt(0.1, rect.adjusted(distance, -distance, 0, 0));
    pAnimation->setKeyValueAt(0.2, rect.adjusted(-distance, distance, 0, 0));
    pAnimation->setKeyValueAt(0.3, rect.adjusted(distance, -distance, 0, 0));
    pAnimation->setKeyValueAt(0.4, rect.adjusted(-distance, distance, 0, 0));
    pAnimation->setKeyValueAt(0.5, rect.adjusted(distance, -distance, 0, 0));
    pAnimation->setKeyValueAt(0.6, rect.adjusted(-distance, distance, 0, 0));
    pAnimation->setKeyValueAt(0.7, rect.adjusted(distance, -distance, 0, 0));
    pAnimation->setKeyValueAt(0.8, rect.adjusted(-distance, distance, 0, 0));
    pAnimation->setKeyValueAt(0.9, rect.adjusted(distance, -distance, 0, 0));
    pAnimation->setKeyValueAt(1, rect.adjusted(0, 0, 0, 0));
    pAnimation->start(QAbstractAnimation::DeleteWhenStopped);

    QObject::connect(pAnimation, &QPropertyAnimation::finished, target, onFinished);
}

// 移动动画
template<typename func>
static inline void animationMove(QWidget *target, int duration, QPoint point, func onFinished = nullptr)
{
    QPropertyAnimation *pAnimation = new QPropertyAnimation(target, "pos");
    pAnimation->setDuration(duration);
    pAnimation->setStartValue(target->pos());
    pAnimation->setEndValue(target->pos() + point);
    pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
    QObject::connect(pAnimation, &QPropertyAnimation::finished, target, onFinished);
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setFixedSize(800, 800);

    // 红色动画按钮
    m_pBtnRed = new QPushButton(this);
    m_pBtnRed->setFixedSize(120, 60);
    m_pBtnRed->setText("红色界面动画");
    m_pBtnRed->move(100, 100);
    // 蓝色动画按钮
    m_pBtnBlue = new QPushButton(this);
    m_pBtnBlue->setFixedSize(120, 60);
    m_pBtnBlue->setText("蓝色界面动画");
    m_pBtnBlue->move(300, 100);
    // 黄色动画按钮
    m_pBtnYellow = new QPushButton(this);
    m_pBtnYellow->setFixedSize(120, 60);
    m_pBtnYellow->setText("黄色界面动画");
    m_pBtnYellow->move(500, 100);

    // 红色界面
    m_pWidgetRed = new QWidget(this);
    m_pWidgetRed->setFixedSize(200, 200);
    m_pWidgetRed->move(QPoint(50, 200));
    m_pWidgetRed->setObjectName("Widget1");
    m_pWidgetRed->setStyleSheet("QWidget#Widget1{background: red;}");
    m_pWidgetRed->show();

    // 蓝色界面
    m_pWidgetBlue = new QWidget(this);
    m_pWidgetBlue->setFixedSize(200, 200);
    m_pWidgetBlue->move(300, 200);
    m_pWidgetBlue->setObjectName("Widget2");
    m_pWidgetBlue->setStyleSheet("QWidget#Widget2{background: blue;}");
    m_pWidgetBlue->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    m_pWidgetBlue->show();

    // 黄色界面
    m_pWidgetYellow = new QWidget(this);
    m_pWidgetYellow->setFixedSize(200, 200);
    m_pWidgetYellow->move(550, 200);
    m_pWidgetYellow->setObjectName("Widget3");
    m_pWidgetYellow->setStyleSheet("QWidget#Widget3{background: yellow;}");
    m_pWidgetYellow->show();

    // 红色动画按钮
    connect(m_pBtnRed, &QPushButton::clicked, [=]{
        // 移动动画
        animationMove(m_pWidgetRed, 300, QPoint(0, 200), [=](){
            qDebug() << "移动动画完成";

            animationMove(m_pWidgetRed, 300, QPoint(0, -200), [=](){
                qDebug() << "移动动画返回原处";
            });
        });
    });

    // 蓝色动画按钮
    connect(m_pBtnBlue, &QPushButton::clicked, [=]{
        // 隐藏动画
        animationOpacity(m_pWidgetBlue, 1.0, 0.0, 800, [=]() {
            qDebug() << "隐藏动画完成";

            animationOpacity(m_pWidgetBlue, 0.0, 1.0, 800, [=]{});
        });
    });

    // 黄色动画按钮
    connect(m_pBtnYellow, &QPushButton::clicked, [=]{
        // 摇晃动画
        animationShake(m_pWidgetYellow, 500, 5, [=](){
            qDebug() << "摇晃动画完成";
        });
    });
}

void Widget::showEvent(QShowEvent *event)
{
    Q_UNUSED(event);

    m_pWidgetBlue->move(this->pos().x() + 300, this->pos().y() + 200 + 30);
}

标签:pAnimation,动画,Qt,distance,之二,QWidget,rect,target
From: https://www.cnblogs.com/linuxAndMcu/p/17109710.html

相关文章

  • qt 卡拉OK 歌词效果
    //思路:一、先绘制全部歌词二、设置裁剪区域三、绘制已经唱的歌词voidWidget::paintEvent(QPaintEvent*event){QLineFline(10.0,80.0,90.0,20.0);QPainter......
  • Abp vnext + MQTT
    目录Artizan.Iot.Hub.Mqtt.Application.ContractsIMqttServiceBaseIMqttConnectionService:负责MqttServer与链接相关初始化IMqttPublishingService:负责MqttServer的发......
  • 解决PyQtWebEngine安装缓慢的问题
    在使用PyQtWebEngine时候,发现pyqt5由于版本高而没有PyQtWebEngine。于是安装但是在使用清华源的时候,发现下载非常慢。我通过各种方式进行测试均需要9小时才能下载。原因未......
  • Qt-Qt实现高斯模糊效果
    相关资料:https://qa.1r1g.com/sf/ask/1044059061/实例代码:.pro1QT+=coregui23greaterThan(QT_MAJOR_VERSION,4):QT+=widgets45CONFIG+......
  • python mqtt服务器搭建
    一.在Linux中搭建mqtt服务环境:Linux版本Ubuntu 18.04.1 LTS1.进入https://www.emqx.com/zh/try?product=broker下载开源版本 EMQX 此处选择zip格式2.下载后将e......
  • 基于QT实现的影院订票系统[2023-02-09]
    基于QT实现的影院订票系统[2023-02-09](1)订票模块:用户选择影院影片和场次,进行选座购票的操作等。(2)用户模块:用户的登录,修改信息等。(3)影院管理模块:影院负责人对自己影院的......
  • 基于QT的校园共享平台的设计与实现[2023-02-09]
    基于QT的校园共享平台的设计与实现[2023-02-09]随着经济的迅速发展和科技日新月异的进步,产品更新换代速度越来越快,物价水平持续高升。大学生作为消费者,但其消费水平不高,消......
  • Qt多线程编程之QThread
    背景引言[GUI主线程+子线程]跟C++11中很像的是,Qt中使用QThread来管理线程,一个QThread对象管理一个线程,在使用上有很多跟C++11中相似的地方,但更多的是Qt中独有的内容......
  • [threeJS]--- 外部导入的模型如何编程式实现帧动画以及调用模型自带的动画
    1.代码中编写帧动画并且调用 asyncfunctionkeyframeAni(object){consttimes=[0,2];//关键帧时间数组,单位'秒'constrorateValues=[0,-Math.PI*2];......
  • QTcpSocket 设置接收数据延时等待时间
    /*客户端接入槽函数*/voidTcpServer::slotNewConnect(void){/*获取连接的客户端句柄这里设置刷新数据时间1ms*/QTcpSocket*pSocket=this->m_pServer->ne......