效果图
#头文件
#ifndef WIFIICON_H
#define WIFIICON_H
#include <QWidget>
class QTimer;
class WifiIcon : public QWidget
{
Q_OBJECT
public:
explicit WifiIcon(QWidget *parent = nullptr);
signals:
public:
/**
* 参数二:半径
* 参数三:开始的角度
* 参数四:指扫取的角度-顺时针(360度 / 8 = 45度)
* 参数五:圆环的高度
* 参数六:填充色
**/
void drawCircularArc(QPainter *painter, int radius, int startAngle, int angleLength, int arcHeight);
double getMinValue() const;
double getMaxValue() const;
void reStart(bool IsOpen);
public slots:
//设置最大最小值
void setMinValue(double minValue);
void setMaxValue(double maxValue);
void setCurrentValue(double currentValue);
void slots_TimeOut();
protected:
void paintEvent(QPaintEvent *);
protected:
double m_MinValue;
double m_MaxValue;
double m_CurrentValue;
bool IsReStart;
QTimer *m_Timer;
};
#endif // WIFIICON_H
#C文件
#include "wifiicon.h"
#include "qpainter.h"
#include "qtimer.h"
#include "qdebug.h"
#include <QTimer>
WifiIcon::WifiIcon(QWidget *parent) : QWidget(parent)
{
IsReStart=false;
m_CurrentValue=100;
m_MinValue=0;
m_MaxValue=100;
m_Timer=new QTimer(this);
connect(m_Timer,&QTimer::timeout,this,&WifiIcon::slots_TimeOut);
}
void WifiIcon::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
double Width = width();
double Height = height();
painter.setPen(Qt::SolidLine);
int minWidHei;
if(Height>Width){
minWidHei=Width;
}else{
minWidHei=Height;
}
double radius_1=minWidHei/9.5;
double radius_2 = Height*0.85;
int arcHeight = Height/10.5;
QColor Color_1(255, 255, 255);
QColor Color_2(100,100,100);
int splitValue=0;
if(m_CurrentValue<50)
{
splitValue=3;
}else if(m_CurrentValue>=50&&m_CurrentValue<60)
{
splitValue=2;
}else if(m_CurrentValue>=60&&m_CurrentValue<70)
{
splitValue=1;
}else if(m_CurrentValue>=70&&m_CurrentValue<80)
{
splitValue=0;
}else if(m_CurrentValue>=80)
{
splitValue=-1;
}
for(int i=0;i<4;i++){
if(i<=splitValue&&splitValue!=-1){
painter.setBrush(Color_1);
painter.setPen(Color_1);
}else{
painter.setBrush(Color_2);
painter.setPen(Color_2);
}
switch (i) {
case 0:
painter.drawEllipse(QPointF(Width*0.5,Height*0.88),radius_1,radius_1);
painter.translate(QPointF(Width*0.5,Height*0.9));//使用QPainter::translate()函数平移坐标系统;实际就是重新设置坐标原点
break;
case 1:
drawCircularArc(&painter, radius_2*0.40,45,90, arcHeight);
break;
case 2:
drawCircularArc(&painter, radius_2*0.70,45,90, arcHeight);
break;
case 3:
drawCircularArc(&painter, radius_2, 45,90, arcHeight);
break;
}
}
}
void WifiIcon::drawCircularArc(QPainter *painter, int radius, int startAngle, int angleLength, int arcHeight)
{
QRectF rect(-radius, -radius, radius<<1, radius<<1);
QPainterPath path;
path.arcTo(rect, startAngle, angleLength);
QPainterPath subPath;
subPath.addEllipse(rect.adjusted(arcHeight, arcHeight, -arcHeight, -arcHeight));
// path为扇形 subPath为椭圆
path -= subPath;
painter->drawPath(path);
// painter->setBrush(Qt::NoBrush);
// painter->drawRect(rect);
// painter->drawRect(rect.adjusted(arcHeight, arcHeight, -arcHeight, -arcHeight));
}
double WifiIcon::getMinValue() const
{
return m_MinValue;
}
double WifiIcon::getMaxValue() const
{
return m_MaxValue;
}
void WifiIcon::reStart(bool IsOpen)
{
if(IsReStart==IsOpen){ return; }
if(IsOpen==true){
m_Timer->start(200);
IsReStart=true;
}else{
m_Timer->stop();
IsReStart=false;
setCurrentValue(100);
}
}
void WifiIcon::setMinValue(double minValue)
{
m_MinValue=minValue;
update();
}
void WifiIcon::setMaxValue(double maxValue)
{
m_MaxValue=maxValue;
update();
}
void WifiIcon::setCurrentValue(double currentValue)
{
if(IsReStart==true){
IsReStart=false;
m_Timer->stop();
}
if(currentValue<0){ currentValue=currentValue*-1; }
m_CurrentValue=currentValue;
update();
}
void WifiIcon::slots_TimeOut()
{
m_CurrentValue-=5;
if(m_CurrentValue<=45){
m_CurrentValue=86;
}
update();
}
源码下载
链接: 源码下载