Qt 简约美观的加载动画 第九季

这次和大家分享6个非常清爽的加载动画.
😊 效果如下 😊
在这里插入图片描述
一共三个文件 , 可以直接编译运行的呢

//main.cpp
#include "LoadingAnimWidget.h"
#include <QApplication>
#include <QGridLayout>
int main(int argc, char *argv[])
{QApplication a(argc, argv);QWidget w;w.setWindowTitle("加载动画 第9季");QGridLayout * mainLayout = new QGridLayout;auto* anim1 = new ThreeRevolvingBalls;mainLayout->addWidget(anim1,0,0);auto* anim2 = new InfinityLoop;mainLayout->addWidget(anim2,0,1);auto* anim3 = new Fingerprint;mainLayout->addWidget(anim3,0,2);auto* anim4 = new Solitary9Swords;mainLayout->addWidget(anim4,1,0);auto* anim5 = new LithiumWave;mainLayout->addWidget(anim5,1,1);auto* anim6 = new DotInCircle;mainLayout->addWidget(anim6,1,2);w.setLayout(mainLayout);w.show();anim1->start();anim2->start();anim3->start();anim4->start();anim5->start();anim6->start();return a.exec();
}
//LoadingAnimWidget.h
#ifndef LOADINGANIMWIDGET_H
#define LOADINGANIMWIDGET_H
#include <QPropertyAnimation>
#include <QWidget>
class LoadingAnimBase:public QWidget
{Q_OBJECTQ_PROPERTY(qreal angle READ angle WRITE setAngle)
public:LoadingAnimBase(QWidget* parent=nullptr);virtual ~LoadingAnimBase();qreal angle()const;void setAngle(qreal an);
public slots:virtual void exec();virtual void start();virtual void stop();
protected:QPropertyAnimation mAnim;qreal mAngle;
};
class ThreeRevolvingBalls:public LoadingAnimBase{
public:ThreeRevolvingBalls(QWidget* parent = nullptr);//三个绕着中心店旋转和缩放的球
protected:void paintEvent(QPaintEvent*);
};class InfinityLoop:public LoadingAnimBase{//一个无穷大标志的双环,上面有一个行星在无尽地绕行
public:InfinityLoop(QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent*);
};
class Fingerprint:public LoadingAnimBase{//一个多层的转动的圆环,像变幻的指纹
public:Fingerprint(QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent*);
};
class Solitary9Swords:public LoadingAnimBase{//六根竖线旋转,中间变幻出18根竖线,有点像独孤九剑在空中的残影
public:Solitary9Swords(QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent*);
};
class LithiumWave:public LoadingAnimBase{//中间是相连的三个小球,它们的转动引起周围波浪发散出去,类似锂离子的三个质子振动
public:LithiumWave(QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent*);
};
class DotInCircle:public LoadingAnimBase{//一个圆圈内部有一个小球绕着内侧轨道转动,模仿的小米手机浏览器的加载动画
public:DotInCircle(QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent*);
};#endif
//LoadingAnimWidget.cpp
#include "LoadingAnimWidget.h"
#include <QDebug>
#include <QPaintEvent>
#include <QPainter>
#include <QtMath>
#include <QRandomGenerator>
LoadingAnimBase::LoadingAnimBase(QWidget* parent):QWidget(parent){mAnim.setPropertyName("angle");mAnim.setTargetObject(this);mAnim.setDuration(2000);mAnim.setLoopCount(-1);//run forevermAnim.setEasingCurve(QEasingCurve::Linear);setFixedSize(200,200);mAngle = 0;
}
LoadingAnimBase::~LoadingAnimBase(){}
void LoadingAnimBase::exec(){if(mAnim.state() == QAbstractAnimation::Stopped){start();}else{stop();}
}
void LoadingAnimBase::start(){mAnim.setStartValue(0);mAnim.setEndValue(360);mAnim.start();
}
void LoadingAnimBase::stop(){mAnim.stop();
}
qreal LoadingAnimBase::angle()const{ return mAngle;}
void LoadingAnimBase::setAngle(qreal an){mAngle = an;update();
}
ThreeRevolvingBalls::ThreeRevolvingBalls(QWidget* parent ):LoadingAnimBase(parent){}
void ThreeRevolvingBalls::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);const qreal x = width();const qreal y = height();painter.setPen(Qt::NoPen);static const QColor colorList[3] = {"cadetblue" , "coral" , "hotpink"};painter.translate(x/2,y/2);painter.rotate(mAngle);for(int i = 0;i < 3;++i){painter.setBrush(QBrush(colorList[i]));const auto r = (0.1+0.03*qSin(M_PI/60*mAngle))*y;painter.drawEllipse(QPointF(0,-y/6),r,r);painter.rotate(120);}
}
InfinityLoop::InfinityLoop(QWidget* parent):LoadingAnimBase (parent){}
void InfinityLoop::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);const qreal x = width();const qreal y = height();const qreal r = 0.45*x / (1+qSqrt(2));static const qreal _sqrt2 = 0.70711;// 1/根号2painter.setBrush(Qt::NoBrush);painter.translate(x/2,y/2);QColor railColor("lightgray");railColor.setAlphaF(0.5);QPen pen(railColor);pen.setWidth(6);painter.setPen(pen);//先画一个淡色的轨道QPainterPath pp;pp.moveTo(0,0);pp.lineTo(_sqrt2*r,-_sqrt2*r);pp.arcTo(QRectF(0.45*x-2*r,-r,2*r,2*r),135,-270);pp.lineTo(-_sqrt2*r,-_sqrt2*r);pp.arcTo(QRectF(-0.45*x,-r,2*r,2*r),45,270);pp.lineTo(0,0);painter.drawPath(pp);//画一个小火车pen.setColor("darkgray");painter.setPen(pen);static const int totPercent = 30;const int rotateAngle = 270;if(mAngle < rotateAngle){for(int i = 0;i < totPercent;++i){qreal percent = mAngle / rotateAngle - 0.01*i;int tmp = percent*100;percent = tmp/100.0;//精度不要太高,否则起点会有抖动if(percent < 0 ) break;painter.drawEllipse( pp.pointAtPercent(percent) , 2,2);}}else{int left = (360-mAngle)/(360-rotateAngle) * totPercent;for(int i = 0;i < left;++i){painter.drawEllipse(pp.pointAtPercent(1 - 0.01*i),2,2);}}
}
Fingerprint::Fingerprint(QWidget* parent):LoadingAnimBase (parent){mAnim.setDuration(8000);
}
void Fingerprint::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);painter.setBrush(Qt::NoBrush);QPen pen("darkseagreen");pen.setWidth(2);pen.setCapStyle(Qt::RoundCap);painter.setPen(pen);const qreal x = width();const qreal y = height();painter.translate(x/2,y/2);static const int loopNum = 12;//12层指纹static const int gap = 6;//指纹的间距static QList<qreal> spanList;if(spanList.size() <= 0){for(int i = 0;i < 361;++i)spanList.push_back(120+60*qSin(M_PI*2/360*i));}for(int i = 0;i < loopNum;++i){const int r = gap*(i+1);const qreal start = mAngle*(i+1);//越往外,速度越快const int idx = mAngle;painter.drawArc(QRectF(-r,-r,r*2,r*2),start*16,16*spanList[idx]);}
}
Solitary9Swords::Solitary9Swords(QWidget* parent):LoadingAnimBase(parent){mAnim.setDuration(4000);
}void Solitary9Swords::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);const qreal x = width();const qreal y = height();painter.translate(x/2,y/2);painter.setBrush(Qt::NoBrush);static const QColor colorList[3] = {"cadetblue" , "lawngreen" , "lightblue"};QPen pen;pen.setCapStyle(Qt::RoundCap);pen.setWidthF(2);if(mAngle < 150){const qreal ratio = mAngle / 150;for(int i = 0;i < 3;++i){qreal speed = ratio;if(i == 0) speed =  qPow(ratio,0.4);//最快else if(i == 1) speed = qPow(ratio,0.6);else if(i == 2) speed = qPow(ratio,0.99);//最慢pen.setColor(colorList[i]);painter.setPen(pen);for(int a = 0;a < 6;a++){painter.rotate(60*speed);painter.drawLine(0,-0.3*y/2,0,-0.9*y/2);}painter.resetTransform();painter.translate(x/2,y/2);}}else if(mAngle < 210){pen.setColor(colorList[2]);painter.setPen(pen);for(int a = 0; a< 6;a++){painter.drawLine(0,-0.3*y/2,0,-0.9*y/2);painter.rotate(60);}}else{const qreal ratio = (mAngle - 210) / 150;pen.setColor(colorList[2]);painter.setPen(pen);painter.drawLine(0,-0.3*y/2,0,-0.9*y/2);for(int i = 0;i < 3;++i){qreal speed;if(i == 0) speed =  qPow(ratio,0.4);//最快else if(i == 1) speed = qPow(ratio,0.6);else if(i == 2) speed = qPow(ratio,0.99);//最慢pen.setColor(colorList[i]);painter.setPen(pen);for(int a = 1;a <= 5;++a){painter.rotate(60*a+ (360-a*60)*speed);painter.drawLine(0,-0.3*y/2,0,-0.9*y/2);painter.resetTransform();painter.translate(x/2,y/2);}}}
}
LithiumWave::LithiumWave(QWidget* parent):LoadingAnimBase(parent){}
void LithiumWave::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);QPen pen("lightblue");pen.setWidth(2);painter.setPen(pen);painter.setBrush(QBrush("lightblue"));const qreal x = width();const qreal y = height();painter.translate(x/2,y/2);static const qreal e = 6;//等边三角形的半边长static const qreal r = 4; //质子半径qreal zoom = 0.5+ 2*qSin(M_PI / 360 * mAngle);painter.scale(zoom,zoom);painter.rotate(mAngle);//画三个质子for(int i = 0;i < 3;++i){painter.drawEllipse(QPointF(0,-sqrt(3) *2/3*e ) , r,r);painter.drawLine(0, -sqrt(3) *2/3*e,e,sqrt(3)*e/3);painter.rotate(120);}//画5层电磁波painter.resetTransform();painter.translate(x/2,y/2);painter.setBrush(Qt::NoBrush);for(int i = 0;i < 5;++i){const qreal ratio = (mAngle - i*40)/200;if(ratio < 0) break;    //最后一层都灭了,后面已经没有波了if(ratio > 1) continue;//最外层的不要画了qreal wr = x/2*(0.3 + 0.7*ratio);QColor c = pen.color();qreal opacity = qSin(M_PI*ratio);c.setAlphaF(opacity);pen.setColor(c);painter.setPen(pen);painter.drawEllipse(QPointF(0,0),wr,wr);}
}
DotInCircle::DotInCircle(QWidget* parent):LoadingAnimBase(parent){}
void DotInCircle::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);painter.setBrush(Qt::NoBrush);QPen pen("lightgray");pen.setWidth(4);painter.setPen(pen);const qreal x = width();const qreal y = height();painter.translate(x/2,y/2);painter.drawEllipse(QPointF(0,0),x/4,x/4);painter.setPen(Qt::NoPen);painter.setBrush(QBrush("darkgray"));painter.rotate(mAngle);static const qreal r = 8;painter.drawEllipse(QPointF(0,-x/4 +r*2),r,r);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/508882.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

css样式元素的相对定位,绝对定位,固定定位等元素定位运用技巧详解

文章目录 1.相对定位 relative2.绝对定位 absolute3.固定定位4.display 转换元素5.float浮动6.float产生内容塌陷问题7.overflow CSS样式学习宝典&#xff0c;关注点赞加收藏&#xff0c;防止迷路哦 在CSS中关于定位的内容是&#xff1a;position:relative | absolute | static…

Java进阶-测试方法

来学习一下软件测试相关的方法&#xff0c;了解一下黑盒测试和白盒测试&#xff0c;以及后面要用到的JUnit单元测试。JUnit单元测试也属于白盒测试&#xff0c;这次内容较少且相对简单。 一、软件测试方法 1、黑盒测试 不需要写代码&#xff0c;给输入值&#xff0c;看程序…

【Tomcat】The CATALINA_HOME environment variable is not defined correctly

文章目录 一、问题二、解决办法三、优化 一、问题 运行绿色版Tomcat时&#xff0c;单击apache-tomcat-9.0.27\bin\startup.bat时窗口一闪而过。 检查JAVA_HOME环境变量&#xff0c;可以发现并没有问题。 为了检查错误&#xff0c;将startup.bat程序使用文本编辑器打开&#x…

单片机独立按键控制LED状态

一、前言 这幅图是按键的抖动与时间的联系 按键抖动&#xff1a;对于机械开关&#xff0c;当机械鮑点断开、闭合时&#xff0c;由于机械触点的弹性作用&#xff0c;一个开关在闭合时不会马上稳定地接通&#xff0c;在断开时也不会一下子断开&#xff0c;所以在开关闭合及断开的…

《JAVA与模式》之调停者模式

系列文章目录 文章目录 系列文章目录前言一、为什么需要调停者二、调停者模式的结构三、使用电脑来看电影 前言 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到网站&#xff0c;这篇文章男女通…

【二分查找】【C++算法】378. 有序矩阵中第 K 小的元素

作者推荐 视频算法专题 本文涉及的基础知识点 二分查找算法合集 LeetCode378. 有序矩阵中第 K 小的元素 给你一个 n x n 矩阵 matrix &#xff0c;其中每行和每列元素均按升序排序&#xff0c;找到矩阵中第 k 小的元素。 请注意&#xff0c;它是 排序后 的第 k 小元素&…

深度学习_15_过拟合欠拟合

过拟合和欠拟合 过拟合和欠拟合是训练模型中常会发生的事&#xff0c;如所要识别手势过于复杂&#xff0c;如五角星手势&#xff0c;那就需要更改高级更复杂的模型去训练&#xff0c;若用比较简单模型去训练&#xff0c;就会导致模型未能抓住手势的全部特征&#xff0c;那简单…

了解游戏中的数据同步

目录 数据同步 通过比较来看状态同步和帧同步 状态同步 帧同步 帧同步实现需要的条件 两者相比较 数据同步 在联机游戏中&#xff0c;我的操作和数据要同步给同一局游戏中其他所有玩家&#xff0c;其他玩家的操作和数据也会同步给我。这叫做数据同步&#xff0c;目前数据…

《TCP/IP详解 卷一》第10章 UDP 和 IP 分片

目录 10.1 引言 10.2 UDP 头部 10.3 UDP校验和 10.4 例子 10.5 UDP 和 IPv6 10.6 UDP-Lite 10.7 IP分片 10.7.1 例子&#xff1a;IPV4 UDP分片 10.7.2 重组超时 10.8 采用UDP的路径MTU发现 10.9 IP分片和ARP/ND之间的交互 10.10 最大UDP数据报长度 10.11 UDP服务器…

代码随想录Day66 | 图的DFS与BFS

代码随想录Day66 | 图的DFS与BFS DFS797.所有可能的路径无向图和有向图的处理 BFS200.岛屿数量 DFS 文档讲解&#xff1a;代码随想录 视频讲解&#xff1a; 状态 本质上就是回溯算法。 void dfs(参数) {if (终止条件) {存放结果;return;}for (选择&#xff1a;本节点所连接的…

猴子吃桃问题(python版)

文章预览&#xff1a; 题目python解法一&#xff1a;运行结果 python解法二&#xff1a;运行结果 python解法三&#xff1a;运行结果 题目 猴子吃桃问题&#xff1a;猴子第一天摘下若干个桃子&#xff0c;当即吃了一半&#xff0c;还不过瘾&#xff0c;又多吃了一个。 第二天早…

超全面!Linux学习资料大合集,21套从入门到进阶,看这篇就够了

本文将为那些渴望学习Linux&#xff0c;但又缺乏相应资料和方向的朋友&#xff0c;提供21套Linux优质资料&#xff0c;包含入门到进阶&#xff0c;希望能对大家有所帮助。 此合集内容及其丰富&#xff0c;涉及方面颇多&#xff0c;不仅适合Linux入门学习的朋友&#xff0c;运维…