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

相关文章

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版本主要

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

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

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

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

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

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

如何使用Python实现一个简单的window任务管理器

《如何使用Python实现一个简单的window任务管理器》这篇文章主要为大家详细介绍了如何使用Python实现一个简单的window任务管理器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 任务管理器效果图完整代码import tkinter as tkfrom tkinter i