上效果
推荐文章:
- qt电池控件设计:https://blog.csdn.net/weixin_42887343/article/details/113932145
- QWidget控件拖动:https://blog.csdn.net/weixin_42887343/article/details/114384324
- QWidget控件旋转方法:https://blog.csdn.net/weixin_42887343/article/details/115037420
- qt淡化提示框设计:https://blog.csdn.net/weixin_42887343/article/details/109303972
- qt之led(点阵)控件类设计:https://blog.csdn.net/weixin_42887343/article/details/115348953
- 加载动画控件类设计:https://blog.csdn.net/weixin_42887343/article/details/118541411
- 环形进度条控件设计:https://blog.csdn.net/weixin_42887343/article/details/120222293
上代码
1、 控件的h文件
#ifndef ROUNDPROGRESSBAR_H
#define ROUNDPROGRESSBAR_H
#include <QWidget>
#include <QPainter>
#define Abs(x) ((x)>=0?(x):-(x))
class RoundProgressBar : public QWidget
{
Q_OBJECT
Q_PROPERTY(float _value READ getValue WRITE _setValue)
public:
enum SwitchFlags
{
defaultText =0x00000001,
linearColor =0x00000004,
decorateDot =0x00000008,
outterCirle =0x0000000e,
animation =0x00000010,
all =0xffffffff
};
enum InnerDefaultTextStyle
{
percent =0x00000001,
value =0x00000002,
valueAndMax =0x00000004
};
explicit RoundProgressBar(QWidget *parent = 0);
~RoundProgressBar();
void setdefault(int,bool);
void setOutterBarWidth(float);
void setInnerBarWidth(float);
void setRange(float, float);
void setValue(float);
void setOutterColor(const QColor&);
void setInnerColor(const QColor&,const QColor&);
void setInnerColor(const QColor&);
void setDefaultTextColor(const QColor&);
void setControlFlags(int);
void setPrecision(int);
inline void setInnerDefaultTextStyle(InnerDefaultTextStyle style){_innerDefaultTextStyle=style;}
inline float getValue(){return _value;}
protected:
void paintEvent(QPaintEvent *);
void resizeEvent(QResizeEvent *event);
private:
int _startAngle;
bool _clockWise;
float _outterBarWidth;
float _innerBarWidth;
float _dotX;
float _dotY;
float _min;
float _max;
float _value;
QColor _outterColor;
QColor _startColor;
QColor _endColor;
QColor _textColor;
int _precision;
float _squareStart;
float _squareWidth;
quint32 _controlFlags=0x00000000;
InnerDefaultTextStyle _innerDefaultTextStyle;
void paintOutterBar(QPainter &);
void paintInnerBar(QPainter &);
void paintDot(QPainter &);
void paintText(QPainter &);
void _setValue(float);
void caculateSquare();
};
#endif
2、控件cpp文件
#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)
{
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);
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);
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;
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;
}
}
使用示例
使用方法很简单,代码如下:
RoundProgressBar* bar = new RoundProgressBar(this);
bar->setOutterBarWidth(22);
bar->setInnerBarWidth(20);
bar->setControlFlags(RoundProgressBar::all);
或者采用提升的方式也行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)