qt-动画圆圈等待-LED数字

news2024/10/5 19:09:06

qt-动画圆圈等待-LED数字

  • 一、演示效果
  • 二、关键程序
  • 三、下载链接

一、演示效果

在这里插入图片描述

二、关键程序

#include "LedNumber.h"
#include <QLabel>

LEDNumber::LEDNumber(QWidget *parent) : QWidget(parent)
{
    //设置默认宽高比
    setScale((float)0.6);
    //设置默认背景色
    setBackColor(QColor(85,85,85));
    //设置默认文字颜色
    setFrontColor(QColor(255,255,255));
    //设置中间线颜色
    setLineColor(QColor(60,60,60));
    //设置默认文字
    setText("2");
    //设置默认文字大小
    setFontSize(40);
    //设置默认最小尺寸
    this->setMinimumSize(100,100);
}

//**********************************************  设置部分 ****************************************
//设置文字颜色
void LEDNumber::setFrontColor(const QColor & color)
{
    _frontColor=color;
}

//设置背景色
void LEDNumber::setBackColor(const QColor & color)
{
    _backColor=color;
}

//设置中间线颜色
void LEDNumber::setLineColor(const QColor& color)
{
    _lineColor=color;
}

//设置宽高比
void LEDNumber::setScale(float scale)
{
    _scale=scale;
}

//设置文字
void LEDNumber::setText(QString text)
{
    _text=text;
}

void LEDNumber::setFontSize(int size)
{
    _fontSize=size;
}

//**********************************************  绘制部分 ****************************************
void LEDNumber::resizeEvent(QResizeEvent *event)
{
    //计算绘制区域
    caculateArea();
}

void LEDNumber::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    //计算绘制区域
//    caculateArea();
    //绘制背景
    drawBack(painter);
    //绘制文字
    drawText(painter);
    //绘制中间线
    drawLine(painter);
}

//计算绘制区域
void LEDNumber::caculateArea()
{
    if((this->height()-2)*_scale>(this->width()-2))
    {
        _width=this->width()-2;
        _height=_width/_scale;
    }
    else
    {
        _height=this->height()-2;
        _width=_height*_scale;
    }
}

//绘制背景
void LEDNumber::drawBack(QPainter & painter)
{
    QPen pen(_backColor,1);
    painter.setPen(pen);
    QPainterPath path;
    path.addRoundedRect(1,1,_width,_height,10,10);
    painter.fillPath(path,_backColor);
    painter.drawPath(path);
}

//绘制文字
void LEDNumber::drawText(QPainter& painter)
{
    QPen pen(_frontColor);
    painter.setPen(pen);
    painter.setFont(QFont("Microsoft YaHei",_fontSize,75));
    painter.drawText(0,0,_width,_height,Qt::AlignCenter,_text);
}

//绘制中间线
void LEDNumber::drawLine(QPainter & painter)
{
    QPen pen(_lineColor,3);
    painter.setPen(pen);
    painter.drawLine(1,_height/2,_width+1,_height/2);
}

#include "Loading.h"
#include "qmath.h"
#include <QDebug>

Loading::Loading(QWidget *parent) : QWidget(parent),_i(0),_interval(50),_index(0)
{
    //设置背景透明
    //this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
    //this->setAttribute(Qt::WA_TranslucentBackground, true);

    setDotColor(QColor(49, 177, 190));
    setDotCount(20);
    connect(&timer,&QTimer::timeout,this,&Loading::refresh);
    setMaxDiameter(30);
    setMinDiameter(5);
}

//********************************************** 设置部分 *************************************
//设置点的个数
void Loading::setDotCount(int count)
{
    _count=count;
}

//设置点的颜色
void Loading::setDotColor(const QColor & color)
{
    _dotColor=color;
}

//开始动画
void Loading::start()
{
    timer.setInterval(_interval);
    timer.start();
}

//设置最大直径
void Loading::setMaxDiameter(float max)
{
    _maxDiameter=max;
}

//设置最小直径
void Loading::setMinDiameter(float min)
{
    _minDiameter=min;
}
//********************************************** 绘制部分 *************************************
//刷新界面
void Loading::refresh()
{
    repaint();
}

void Loading::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event)
    caculate();
}

void Loading::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    painter.setPen(_dotColor);
    painter.setBrush(_dotColor);

    //绘制点
    paintDot(painter);
}

//计算绘制正方形区域
void Loading::caculate()
{
    _squareWidth=qMin(this->width(),this->height());
    float half=_squareWidth/2;
    _centerDistance=half-_maxDiameter/2-1;

    float gap=(_maxDiameter-_minDiameter)/(_count-1)/2;
    float angleGap=(float)360/_count;

    locationList.clear();
    radiiList.clear();

    for(int i=0;i<_count;i++)
    {
        radiiList<<_maxDiameter/2-i*gap;
        float radian=qDegreesToRadians(-angleGap*i);
        locationList.append(Location(half+_centerDistance*qCos(radian),half-_centerDistance*qSin(radian)));
    }
}

//绘制圆点
void Loading::paintDot(QPainter& painter)
{
    for(int i=0;i<_count;i++)
    {
        painter.setPen(_dotColor);
        //半径
        float radii=radiiList.at((_index+_count-i)%_count);

        //绘制圆点
        painter.drawEllipse(QPointF(locationList.at(i).x,locationList.at(i).y),radii,radii);
        //绘制正方形
        //painter.drawRect(locationList.at(i).x,locationList.at(i).y,radii,radii);
        //绘制文字
        //QFont font("Microsoft YaHei",radii*1.2,75);
        //painter.setFont(font);
        //painter.drawText(QPointF(locationList.at(i).x,locationList.at(i).y),u8"霞");
    }
    _index++;
}

#include "roundprogressbar.h"
#include "qmath.h"
#include <QPropertyAnimation>
#include <QDebug>

RoundProgressBar::RoundProgressBar(QWidget *parent) :
    QWidget(parent),_value(0),_min(0),_max(100),_precision(0)
{
    //设置初始角度,顺时针逆时针
    setdefault(90,true);
    //设置默认外圈宽度
    setOutterBarWidth(18);
    //设置默认内圈宽度
    setInnerBarWidth(16);
    //设置默认范围
    setRange(0,100);
    //设置默认值
    setValue(75);
    //设置外圈颜色
    setOutterColor(QColor(233,248,248));
    //设置默认渐变色
    setInnerColor(QColor(49, 177, 190),QColor(133, 243, 244));
    //设置默认文字颜色
    setDefaultTextColor(QColor(49,177,190));
    //设置默认精度
    setPrecision(0);
    //设置内圈默认文字样式
    setInnerDefaultTextStyle(RoundProgressBar::percent);
}

RoundProgressBar::~RoundProgressBar()
{
}

//********************************************** 外部接口 ****************************************
//设置初始角度,顺时针逆时针
void RoundProgressBar::setdefault(int startAngle,bool clockWise)
{
    _startAngle=startAngle;
    _clockWise=clockWise;
}

//设置外圈宽度
void RoundProgressBar::setOutterBarWidth(float width)
{
    _outterBarWidth=width;
}
//设置内圈宽度
void RoundProgressBar::setInnerBarWidth(float width)
{
    _innerBarWidth=width;
}

//设置值的范围
void RoundProgressBar::setRange(float min,float max)
{
    //todo 和value比较
    if(max<min)
    {
        max=100;
        min=0;
    }
    else
    {
        _max=max;
        _min=min;
    }
}

//设置当前值
void RoundProgressBar::setValue(float value)
{
    QPropertyAnimation* animation=new QPropertyAnimation(this,"_value");
    animation->setDuration(500);
    animation->setStartValue(_value);
    animation->setEndValue(value);
    animation->setEasingCurve(QEasingCurve::OutQuad);
    animation->start();
}

void RoundProgressBar::_setValue(float value)
{
    _value=value;
    repaint();
}

//设置外圈颜色
void RoundProgressBar::setOutterColor(const QColor& outterColor)
{
    _outterColor=outterColor;
}

//设置内圈渐变色
void RoundProgressBar::setInnerColor(const QColor& startColor,const QColor& endColor)
{
    _startColor=startColor;
    _endColor=endColor;
}

//设置内圈渐变色
void RoundProgressBar::setInnerColor(const QColor& startColor)
{
    _startColor=startColor;
}

void RoundProgressBar::setDefaultTextColor(const QColor& textColor)
{
    _textColor=textColor;
}

//设置控制
void RoundProgressBar::setControlFlags(int flags)
{
    this->_controlFlags|=flags;
}

//设置显示数字精度
void RoundProgressBar::setPrecision(int precision)
{
    _precision=precision;
}

//********************************************** 内部绘制部分 ****************************************
void RoundProgressBar::resizeEvent(QResizeEvent *event)
{
    //根据内外圈宽度设置控件最小大小
    if(_outterBarWidth>_innerBarWidth)
        this->setMinimumSize(_outterBarWidth*8,_outterBarWidth*8);
    else
        this->setMinimumSize(_innerBarWidth*8,_innerBarWidth*8);
    //计算绘制正方形区域信息
    caculateSquare();
}

void RoundProgressBar::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    //绘制外圈
    paintOutterBar(painter);
    //绘制内圈
    paintInnerBar(painter);
    //绘制外圈
    paintDot(painter);
    //绘制文字
    paintText(painter);
}

//计算绘制正方形区域信息
void RoundProgressBar::caculateSquare()
{
    int minWidth=qMin(this->width(),this->height());
    float barWidth=qMax(_outterBarWidth,_innerBarWidth);
    _squareWidth=minWidth-barWidth-2;
    _squareStart=barWidth/2+1;
    _dotX=_squareStart+_squareWidth/2;
    _dotY=_squareStart;
}

//绘制外圈
void RoundProgressBar::paintOutterBar(QPainter &painter)
{
    if(!(_controlFlags&outterCirle))
        return;
    QPen pen;
    pen.setWidth(_outterBarWidth);
    pen.setColor(_outterColor);
    painter.setPen(pen);
    QRectF rectangle(_squareStart,_squareStart,_squareWidth,_squareWidth);
    //从90度开始,逆时针旋转
    painter.drawEllipse(rectangle);
}

//绘制内圈
void RoundProgressBar::paintInnerBar(QPainter& painter)
{
    QPen pen;
    if(!(_controlFlags&linearColor))
        pen.setColor(_startColor);
    else
    {
        QLinearGradient gradient(0, 0, 0, _squareWidth);
        gradient.setColorAt(0, _startColor);
        gradient.setColorAt(1, _endColor);
        QBrush brush(gradient);
        pen.setBrush(brush);
    }
    pen.setWidth(_innerBarWidth);
    pen.setStyle(Qt::SolidLine);
    pen.setCapStyle(Qt::RoundCap);
    pen.setJoinStyle(Qt::RoundJoin);
    painter.setPen(pen);
    QRectF rectangle(_squareStart,_squareStart,_squareWidth,_squareWidth);
    //从90度开始,逆时针旋转
    int startAngle=_startAngle*16;
    int spanAngle=(_value-_min)/(_max-_min)*360*16*(_clockWise?-1:1);
    painter.drawArc(rectangle,startAngle,spanAngle);
}

//绘制装饰圆点
void RoundProgressBar::paintDot(QPainter& painter)
{
    if(!(_controlFlags&decorateDot))
        return;
    //当bar宽度小于3时,便不再绘制装饰圆点
    if(_innerBarWidth<3)
        return;
    painter.setPen(QColor(255,255,255));
    painter.setBrush(QColor(255,255,255));
    //区域为圆点绘制正方形区域
    painter.drawEllipse(_dotX-_innerBarWidth/6,_dotY-_innerBarWidth/6,_innerBarWidth/3,_innerBarWidth/3);
}

//绘制默认内置文字
void RoundProgressBar::paintText(QPainter& painter)
{
    if(!(_controlFlags&defaultText))
        return;
    painter.setPen(_textColor);
    painter.setFont(QFont("Microsoft YaHei",22,75));
    switch (_innerDefaultTextStyle) {
    case value:
        painter.drawText(_squareStart,_squareStart,_squareWidth,_squareWidth,Qt::AlignCenter,QString::number(_value,'f',_precision));
        break;
    case valueAndMax:
        painter.drawText(_squareStart,_squareStart,_squareWidth,_squareWidth,Qt::AlignCenter,
                         QString::number(_value,'f',_precision)+"/"+QString::number(_max,'f',_precision));
        break;
    case percent:
        painter.drawText(_squareStart,_squareStart,_squareWidth,_squareWidth,Qt::AlignCenter,
                         QString::number(_value/_max*100,'f',_precision)+"%");
        break;
    default:
        break;
    }
}



三、下载链接

https://download.csdn.net/download/u013083044/88864197

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

fastjson解析自定义get方法导致空指针问题

背景 为了在日志中把出入参打印出来&#xff0c;以便验证链路和排查问题&#xff0c;在日志中将入参用fastjson格式化成字符串输出&#xff0c;结果遇到了NPE。 问题复现 示例代码 public static void main(String[] args) {OrganizationId orgId new OrganizationId();N…

HTML+CSS+JS:花瓣登录组件

效果演示 实现了一个具有动态花朵背景和简洁登录框的登录页面效果。 Code <section><img src"./img/background.jpeg" class"background"><div class"login"><h2>Sign In</h2><div class"inputBox"…

CoordConv(NeurIPS 2018)

paper&#xff1a;An Intriguing Failing of Convolutional Neural Networks and the CoordConv Solution official implementation&#xff1a;https://github.com/uber-research/coordconv 存在的问题 本文揭示并分析了CNN在两种不同类型空间表示之间转换能力的欠缺&#…

新书推荐:《分布式商业生态战略:未来数字商业新逻辑与企业数字化转型新策略》

近两年&#xff0c;商业经济环境的不确定性越来越明显&#xff0c;市场经济受到疫情、技术、政策等多方因素影响越来越难以预测&#xff0c;黑天鹅事件时有发生。在国内外经济方面&#xff0c;国际的地缘政治对商业经济产生着重大的影响&#xff0c;例如供应链中断&#xff0c;…

这才是No.1的门禁管理技巧!赶紧抄作业

随着社会的不断发展和科技的飞速进步&#xff0c;安全管理成为各个领域不可或缺的重要环节。在这个背景下&#xff0c;门禁监控系统作为一种先进而高效的安全管理工具逐渐受到了广泛关注和应用。 客户案例 企业大厦管理 在江苏某繁忙的商业大厦中&#xff0c;管理人员常常面临…

Elastic Stack--01--简介、安装

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 1. Elastic Stack 简介为什么要学习ESDB-Engines搜索引擎类数据库排名常年霸榜![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/051342a83f574c8c910cda…

尝试一下最新的联合办公利器ONLYOffice

下载下来一起试试吧 桌面安装版下载地址&#xff1a;https://www.onlyoffice.com/zh/download-desktop.aspx) 官网地址&#xff1a;https://www.onlyoffice.com 普通Office对联合办公的局限性 普通Office软件&#xff08;如Microsoft Office、Google Docs等&#xff09;在面对…

记一次 migo 报错 M7097 没有可用于物料 XXX 的库存过账

背景:公司重构SAP后&#xff0c;引入返利物料&#xff0c;此部分物料的数量统计单位是USD/CNY,不启用会计类视图&#xff0c;但是启用批次管理&#xff0c;但是正常物料不启用批次管理。这是大背景&#xff0c;物料类型为ZZZZ 但是实际需要的是 检查物料还是没有被用作其他方…

什么是web组态?

一、web组态的定义和背景 在深入探讨之前&#xff0c;我们先回顾一下“组态”的定义。在工业自动化领域&#xff0c;组态软件是用于创建监控和数据采集&#xff08;SCADA&#xff09;系统的工具&#xff0c;它允许工程师构建图形界面&#xff0c;实现与各种设备和机器的数据交互…

玩转网络抓包利器:Wireshark常用协议分析讲解

Wireshark是一个开源的网络协议分析工具&#xff0c;它能够捕获和分析网络数据包&#xff0c;并以用户友好的方式呈现这些数据包的内容。Wireshark 被广泛应用于网络故障排查、安全审计、教育及软件开发等领域。关于该工具的安装请参考之前的文章&#xff1a;地址 &#xff0c;…

本地配置多个git账户及ll设置

本地配置多个git账户 清除全局配置将命令行&#xff0c;切换到ssh目录生成GitLab和Gitee的公钥、私钥去对应的代码仓库添加 SSH Keys添加私钥ll设置 管理密钥验证仓库配置关于gitgitee.com: Permission denied (publickey) 清除全局配置 此步骤可以不做&#xff0c;经测试不影…

ElasticSearch聚合操作

目录 ElasticSearch聚合操作 基本语法 聚合的分类 后续示例数据 Metric Aggregation Bucket Aggregation ES聚合分析不精准原因分析 提高聚合精确度 ElasticSearch聚合操作 Elasticsearch除搜索以外&#xff0c;提供了针对ES 数据进行统计分析的功能。聚合(aggregation…

vue里echarts的使用:画饼图和面积折线图

vue里echarts的使用,我们要先安装echarts,然后在main.js里引入: //命令安装echarts npm i echarts//main.js里引入挂载到原型上 import echarts from echarts Vue.prototype.$echarts = echarts最终我们实现的效果如下: 头部标题这里我们封装了一个全局公共组件common-he…

(AISG)M16圆形连接器高强度工业应用互连选型

什么是M16连接器 M16连接器又称C09圆形连接器&J09圆形连接器作为连接设备的一种&#xff0c;其优点是结构紧凑、使用方便、芯数丰富&#xff08;2PIN、3PIN、4PIN、5PIN、6PIN、7PIN、8PIN、12PIN、14PIN、16PIN、19PIN、24PIN&#xff09;。因此&#xff0c;圆形连接器在…

Shader基础的简单实现(基于URP渲染)

一个模型是很多个顶点组成&#xff0c;顶点数据中包含坐标、法线、切线、UV坐标、顶点颜色等等组成。 URP(Universal Render Pipeline)通用渲染管线&#xff0c;是Unity在2019.3版本之后推出的一种新的渲染管线。传统的渲染管线在渲染多光源的情况&#xff0c;是把每一个主要光…

TensorFlow训练大模型做AI绘图,需要多少的GPU算力支撑

TensorFlow训练大模型做AI绘图&#xff0c;需要多少的GPU算力支撑&#xff01;这个问题就涉及到了资金投资的额度了。众所周知&#xff0c;现在京东里面一个英伟达的显卡&#xff0c;按照RTX3090(24G显存-涡轮风扇&#xff09;版本报价是7000-7500之间。如果你买一张这样的单卡…

K8S故障处理指南:网络问题排查思路

1. 前言 对于私有化环境&#xff0c;客户的网络架构&#xff0c;使用的云平台存在着各种差异&#xff0c;K8S网络可能会出现各种问题&#xff0c;此文着重讲解遇到此种问题的排查方法和思路&#xff0c;不会涉及相关网络底层技术描述. 环境说明 由于我们的k8s网络组件默认使…

赞:java使用easy-excel导入数据的通用模板思路

我们在项目中都会有导入导出的功能&#xff0c;这篇文章主要是讲导出的&#xff0c;导入我会在另外一篇博客文章中讲解。 现在我们开始。 首先&#xff1a;需要在项目中的pom.xml中导入easy-excel的依赖 <!--使用esay-excel进行导入导出 --> <dependency> &…

TypeScript01:安装TypeScript

一、TypeScript 官方网站&#xff1a;https://www.tslang.cn/docs/index.html 练习场&#xff1a;https://www.typescriptlang.org/zh/play 好处&#xff1a; 强类型语言&#xff0c;对JS弱类型的一个良好补充&#xff1b;TS利于大型项目团队合作&#xff0c;可以一定程度…

LeetCode 热题 100 | 二叉树(下)

目录 1 114. 二叉树展开为链表 2 105. 从前序与中序遍历序列构造二叉树 3 437. 路径总和 III 菜鸟做题&#xff08;即将返校版&#xff09;&#xff0c;语言是 C 1 114. 二叉树展开为链表 题眼&#xff1a;展开后的单链表应该与二叉树 先序遍历 顺序相同。 而先序遍历就…