#include "qmyroundprogress.h" #include #include #include #include QMyRoundProgress::QMyRoundProgress(QWidget *parent) : QWidget(parent) { QTimer* timer = new QTimer(this); timer->start(50); connect(timer,&QTimer::timeout,this,&QMyRoundProgress::OnTimerd); } void QMyRoundProgress::OnTimerd() { nCount++; update(); } void QMyRoundProgress::paintEvent(QPaintEvent *event) { //控件的绘制工具,通过该工具我们可以进行常用的绘制了 QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); //取得画布大小和中心方形。 int w = this->rect().width(); int h = this->rect().height(); int s; int wmargin=0; int hmargin=0; if (w>h) { s= h; wmargin=(w-s)/2; }else{ s= w; hmargin=(h-s)/2; } //画边框 QPen pen0(QColor("#4D9CF8")); painter.setPen(pen0); painter.drawRoundRect(this->rect(),5,5); int thick = s/20; QPen pen1(QColor(80,80,80)); //绘制弧线的背景 pen1.setWidth(thick); pen1.setCapStyle(Qt::RoundCap); // pen.setColor(QColor("#4D9CF8")); painter.setPen(pen1); QRectF in_rectangle(wmargin+10, hmargin+s/4, s-20, s-20); int in_startAngle = (0) * 16; int in_spanAngle = (180)*16; painter.drawArc(in_rectangle,in_startAngle,in_spanAngle); // in_startAngle = (nOffset + 90+270) * 16; // in_spanAngle = (45)*16; // painter.drawArc(in_rectangle,in_startAngle,in_spanAngle); int nOffset = nCount*5 % 180; position = nOffset *range /180; QPen pen2(QColor("#4D9CF8")); pen2.setWidth(thick); pen2.setCapStyle(Qt::RoundCap); QRectF sec_rectangle(wmargin+10, hmargin+s/4, s-20, s-20); int sec_startAngle = 180 * 16; int sec_spanAngle = -1*((position / range)*180)*16; QConicalGradient gradient; gradient.setCenter(sec_rectangle.center()); gradient.setAngle(190); gradient.setColorAt(0, color2); gradient.setColorAt(0.5, color2); gradient.setColorAt(1, color1); // int arcLengthApproximation = m_width + m_width / 3; QPen pen3(QBrush(gradient), thick); pen3.setCapStyle(Qt::RoundCap); painter.setPen(pen3); painter.drawArc(sec_rectangle,sec_startAngle,sec_spanAngle); //绘制文本,绘制白色的文字 painter.setPen(QPen(QColor(255,255,255))); QString s1; s1=sTitle; //显示标题 QRect rect2(wmargin + 20, hmargin +10, s-40,s/8); painter.setFont(QFont("Microsoft YaHei",s/12)); // painter.drawText(this->rect(),s1); painter.drawText(rect2,Qt::AlignCenter,s1); //显示前后刻度 painter.setFont(QFont("Microsoft YaHei",s/18)); s1="0"; // painter.drawText(this->rect(),s1); rect2.setRect(wmargin,hmargin+s*3/4,20,s/16); painter.drawText(rect2,Qt::AlignCenter,s1); s1=QString::number(range); // painter.drawText(this->rect(),s1); rect2.setRect(wmargin+s-30,hmargin+s*3/4,40,s/16); painter.drawText(rect2,Qt::AlignCenter,s1); //显示当前数量 painter.setFont(QFont("Microsoft YaHei",s/8)); s1=QString::number(position); // painter.drawText(this->rect(),s1); rect2.setRect(wmargin+10,hmargin+10-s/20,s-20,s-20); painter.drawText(rect2,Qt::AlignCenter,s1); //显示单位 painter.setFont(QFont("Microsoft YaHei",s/16)); s1=sUnit; // painter.drawText(this->rect(),s1); rect2.setRect(wmargin+10,hmargin+10+s/10,s-20,s-20); painter.drawText(rect2,Qt::AlignCenter,s1); return; // QRect rect2(wmargin + 20, hmargin +20, s-40,s-40); //添加绘制的字体和字号 s1=QString::number(int(position)); int l = s1.length(); double fr = sqrt(l*l + 4); painter.setFont(QFont("Microsoft YaHei",(s-20)/fr)); // painter.drawText(this->rect(),s1); painter.drawText(rect2,Qt::AlignCenter,s1); } void QMyRoundProgress::SetRange(int n) { range=n; } void QMyRoundProgress::SetPosition(float n) { position=n; update(); } void QMyRoundProgress::SetTitle(QString title) { sTitle = title; } void QMyRoundProgress::SetUnit(QString unit) { sUnit = unit; } void QMyRoundProgress::SetColor(QColor Start,QColor End) { color1 = Start; color2 = End; }