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

相关文章

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringBoot中配置文件的加载顺序解读

《SpringBoot中配置文件的加载顺序解读》:本文主要介绍SpringBoot中配置文件的加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot配置文件的加载顺序1、命令⾏参数2、Java系统属性3、操作系统环境变量5、项目【外部】的ap

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

SpringBoot项目启动报错"找不到或无法加载主类"的解决方法

《SpringBoot项目启动报错找不到或无法加载主类的解决方法》在使用IntelliJIDEA开发基于SpringBoot框架的Java程序时,可能会出现找不到或无法加载主类com.example.... 目录一、问题描述二、排查过程三、解决方案一、问题描述在使用 IntelliJ IDEA 开发基于

Qt 中 isHidden 和 isVisible 的区别与使用小结

《Qt中isHidden和isVisible的区别与使用小结》Qt中的isHidden()和isVisible()方法都用于查询组件显示或隐藏状态,然而,它们有很大的区别,了解它们对于正确操... 目录1. 基础概念2. 区别清见3. 实际案例4. 注意事项5. 总结1. 基础概念Qt 中的 isHidd