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);elsethis->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