Qt QWidget 简约美观的加载动画 第五季 - 小方块风格

2024-02-27 05:52

本文主要是介绍Qt QWidget 简约美观的加载动画 第五季 - 小方块风格,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

给大家分享两个小方块风格的加载动画
😊 第五季来啦 😊 效果如下:
在这里插入图片描述

一个三个文件,可以直接编译运行

//main.cpp
#include "LoadingAnimWidget.h"
#include <QApplication>
#include <QGridLayout>
int main(int argc, char *argv[])
{QApplication a(argc, argv);QWidget w;w.setWindowTitle("加载动画 第5季");QGridLayout * mainLayout = new QGridLayout;auto* anim1= new RhombusShift;mainLayout->addWidget(anim1,0,0);auto* anim2 = new TiltingBricks;mainLayout->addWidget(anim2,0,1);w.setLayout(mainLayout);w.show();anim1->start();anim2->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 RhombusShift:public LoadingAnimBase{//做斜向平移的四个菱形
public:explicit RhombusShift(QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent*);
};
class TiltingBricks:public LoadingAnimBase{//三个正方形一个接一个倾斜倒向右侧
public:explicit TiltingBricks(QWidget* parent = nullptr);
protected:void paintEvent(QPaintEvent*);
};#endif // LOADINGANIMWIDGET_H
//LoadingAnimWidget.cpp
#include "LoadingAnimWidget.h"
#include <QDebug>
#include <QPaintEvent>
#include <QPainter>
#include <QtMath>
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();
}RhombusShift::RhombusShift(QWidget* parent):LoadingAnimBase (parent){mAnim.setDuration(4800);
}
void RhombusShift::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);painter.setPen(Qt::NoPen);const int x = width();const int y = height();static const int cornerGap = 4;//菱形内部顶点和中心点的距离static const int edgeGap = 2;//菱形顶点和外边框的距离const qreal edgeLen = (x/2 - cornerGap - edgeGap) / 1.42;// 菱形的边长const int halfDiagonalx = (x/2 - edgeGap - cornerGap )/2;//水平方向一半对角线长度const int halfDiagonaly = (y/2 - edgeGap - cornerGap )/2;//垂直方向一半对角线长度const QPointF point1(x/2,edgeGap + halfDiagonaly);          //上方const QPointF point2(x/2 + cornerGap + halfDiagonalx , y/2);//右侧const QPointF point3(x/2, y/2 + cornerGap + halfDiagonaly); //下方const QPointF point4(edgeGap + halfDiagonalx,y/2);          //左侧const QList<QPointF> pointList{point1,point2,point3,point4,point1,point2,point3,point4};QPainterPath pathList[4];for(int i = 0;i < 4; ++i){auto & path = pathList[i];path.moveTo(pointList[i]);path.lineTo(pointList[i+1]);path.lineTo(pointList[i+2]);path.lineTo(pointList[i+3]);path.lineTo(pointList[i+4]);}static const QColor brushList[4] = {"lightblue" , "cadetblue" , "lightblue" , "cadetblue"};static const int staticTime = 15;//每次移动到四个节点上面,要静止一段时间,这个值在0-90之间for(int i = 0;i < 4;++i){qreal proportion = 0;const auto rest = fmod(mAngle,90);//余数const auto quotient = (int)mAngle / 90 * 0.25;//商if(rest > 90 - staticTime) proportion = quotient + 0.25;else proportion = rest / (90 - staticTime) * 0.25 + quotient;const QPointF center = pathList[i].pointAtPercent(proportion);painter.translate(center);painter.rotate(45);painter.setBrush(QBrush(brushList[i]));painter.drawRect(-edgeLen/2,-edgeLen/2,edgeLen,edgeLen);painter.resetTransform();}
}
TiltingBricks::TiltingBricks(QWidget* parent):LoadingAnimBase (parent){mAnim.setDuration(4000);
}
void TiltingBricks::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);painter.setPen(Qt::NoPen);static const QColor brushList[3] = {"lightcoral" , "lightblue" , "khaki"};const int x = width();const int y = height();painter.translate(x/2,y/2);const int edgeGap = 4;const int cornerGap = 2;const int edgeLen = x/3 - edgeGap - cornerGap;const QRectF point1(-edgeLen-cornerGap,-edgeLen-cornerGap,edgeLen,edgeLen);//左上const QRectF point2(-edgeLen-cornerGap,cornerGap,edgeLen,edgeLen);//左下const QRectF point3(cornerGap,cornerGap,edgeLen,edgeLen);//右下const QRectF point4(cornerGap,-cornerGap-edgeLen,edgeLen,edgeLen);//右上const QList<QRectF> baseRectList{point1,point2,point3};qreal ang = mAngle;int round = (int)ang / 90;if(round >= 4) round = 0;ang = fmod(ang,90);const int rectIdx = (int)ang / 30;ang = fmod(ang,30) * 3;painter.rotate(90*round);for(int i = 0;i < 3;++i){painter.setBrush(QBrush(brushList[i]));if(i == rectIdx){painter.rotate(ang);painter.drawRoundedRect(baseRectList[i],4,4);painter.rotate(-ang);}else{if(i < rectIdx){painter.rotate(90);painter.drawRoundedRect(baseRectList[i],4,4);painter.rotate(-90);}else{painter.drawRoundedRect(baseRectList[i],4,4);}}}
}

这篇关于Qt QWidget 简约美观的加载动画 第五季 - 小方块风格的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/751394

相关文章

Spring如何使用注解@DependsOn控制Bean加载顺序

《Spring如何使用注解@DependsOn控制Bean加载顺序》:本文主要介绍Spring如何使用注解@DependsOn控制Bean加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录1.javascript 前言2. 代码实现总结1. 前言默认情况下,Spring加载Bean的顺

Qt如何实现文本编辑器光标高亮技术

《Qt如何实现文本编辑器光标高亮技术》这篇文章主要为大家详细介绍了Qt如何实现文本编辑器光标高亮技术,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录实现代码函数作用概述代码详解 + 注释使用 QTextEdit 的高亮技术(重点)总结用到的关键技术点应用场景举例示例优化建议

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

使用Python获取JS加载的数据的多种实现方法

《使用Python获取JS加载的数据的多种实现方法》在当今的互联网时代,网页数据的动态加载已经成为一种常见的技术手段,许多现代网站通过JavaScript(JS)动态加载内容,这使得传统的静态网页爬取... 目录引言一、动态 网页与js加载数据的原理二、python爬取JS加载数据的方法(一)分析网络请求1

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

IDEA下"File is read-only"可能原因分析及"找不到或无法加载主类"的问题

《IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题》:本文主要介绍IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题,具有很好的参... 目录1.File is read-only”可能原因2.“找不到或无法加载主类”问题的解决总结1.File

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言