Qt 简约又简单的加载动画 第七季 音量柱风格

2024-03-03 11:52

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

今天和大家分享两个音量柱风格的加载动画,这次的加载动画的最大特点就是简单,只有几行代码. 效果如下:
在这里插入图片描述
一共三个文件,可以直接编译运行

//main.cpp
#include "LoadingAnimWidget.h"
#include <QApplication>
#include <QGridLayout>
int main(int argc, char *argv[])
{QApplication a(argc, argv);QWidget w;w.setWindowTitle("加载动画 第7季");QGridLayout * mainLayout = new QGridLayout;auto* anim1= new MagnitudeMeter;mainLayout->addWidget(anim1,0,0);auto* anim2 = new MagnitudeMeter;mainLayout->addWidget(anim2,0,1);anim2->setColor("lightblue");auto* anim3 = new MagnitudeMeter;mainLayout->addWidget(anim3,0,2);anim3->setColor("slateblue");auto* anim4 = new ThreeColumn;mainLayout->addWidget(anim4,1,0);auto* anim5 = new ThreeColumn;mainLayout->addWidget(anim5,1,1);anim5->setColor("lightblue");auto* anim6 = new ThreeColumn;mainLayout->addWidget(anim6,1,2);anim6->setColor("slateblue");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 MagnitudeMeter:public LoadingAnimBase{//一个类似音量检测器的动画,有一排小柱子,它们的高度随时变化
public:MagnitudeMeter(QWidget* parent = nullptr);void setColor(const QColor& color);
protected:void paintEvent(QPaintEvent*);
private:QColor mColor;
};
class ThreeColumn:public LoadingAnimBase{//上一个的简化版,三个循环变化高度的小柱子
public:ThreeColumn(QWidget* parent = nullptr);void setColor(const QColor& color);
protected:void paintEvent(QPaintEvent*);
private:QColor mColor;
};
#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();
}MagnitudeMeter::MagnitudeMeter(QWidget* parent):LoadingAnimBase(parent),mColor("cadetblue"){mAnim.setDuration(8000);
}
void MagnitudeMeter::setColor(const QColor& color){if(mColor != color){mColor = color;update();}
}void MagnitudeMeter::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);painter.setPen(Qt::NoPen);painter.setBrush(QBrush(mColor));const qreal x = width();const qreal y = height();static const int amount = 8;//8个小柱子static const int gap = 2;//小柱子之间的间距const qreal w = (0.667*x - (amount-1)*gap) / amount;painter.translate(x/6,0.667*y);static QList<qreal> offsetList;static QList<qreal> factorList;if(offsetList.size() <= 0){QRandomGenerator g;for(int i = 0;i < amount;++i) offsetList.push_back(g.bounded(1.0) * 2*M_PI);for(int i = 0;i < amount;++i) factorList.push_back(g.bounded(4) + 1);//周期不一样}for(int i = 0;i < amount;++i){const int maxh = y/3;const int h = (1+qSin((2*M_PI/360 * mAngle * factorList[i]) + offsetList[i]))/2 * 0.8*maxh+0.2*maxh;painter.drawRect(i*(gap + w),-h,w,h);}
}
ThreeColumn::ThreeColumn(QWidget* parent):LoadingAnimBase (parent),mColor("cadetblue"){}
void ThreeColumn::setColor(const QColor& color){if(mColor != color){mColor = color;update();}
}
void ThreeColumn::paintEvent(QPaintEvent*){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);painter.setPen(Qt::NoPen);painter.setBrush(QBrush(mColor));const qreal x = width();const qreal y = height();painter.translate(x/2,y/2);static const int w = 8;static const int h = 16;static const int gap = 4;qreal h1 = h/2+h/2*qSin(-2*M_PI/360*mAngle);qreal h2 = h/2+h/2*qSin(-2*M_PI/360*mAngle + M_PI*2/3);qreal h3 = h/2+h/2*qSin(-2*M_PI/360*mAngle + M_PI*4/3);qreal yList[3] = {-h1,-h2,-h3};qreal xList[3] = {-1.5*w-gap,-0.5*w,0.5*w+gap};for(int i = 0;i < 3;++i){painter.drawRect(QRectF(xList[i],yList[i],w,-2*yList[i]));}
}

这篇关于Qt 简约又简单的加载动画 第七季 音量柱风格的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

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

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

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

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

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

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

windows和Linux安装Jmeter与简单使用方式

《windows和Linux安装Jmeter与简单使用方式》:本文主要介绍windows和Linux安装Jmeter与简单使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows和linux安装Jmeter与简单使用一、下载安装包二、JDK安装1.windows设

Qt之QMessageBox的具体使用

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