#include "qmytimedisplay.h"
|
#include <QPen>
|
#include <QBrush>
|
#include <QPainter>
|
#include <QTimer>
|
|
QMyTimeDisplay::QMyTimeDisplay(QWidget *parent) : QWidget(parent)
|
{
|
QTimer* timer = new QTimer(this);
|
timer->start(50);
|
connect(timer,&QTimer::timeout,this,&QMyTimeDisplay::OnTimerd);
|
|
}
|
void QMyTimeDisplay::OnTimerd()
|
{
|
nCount++;
|
repaint();
|
}
|
|
void QMyTimeDisplay::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;
|
}
|
int thick=s/20;
|
int gap=thick/2;
|
QPen pen(QColor(0,0,0));
|
//绘制秒钟的大圈
|
pen.setWidth(thick);
|
pen.setColor(QColor("#4D9CF8"));
|
painter.setPen(pen);
|
|
int nOffset = nCount*5 % 360;
|
QRectF in_rectangle(wmargin+10+thick+gap, hmargin+10+thick+gap, s-20-thick*2-gap*2, s-20-thick*2-gap*2);
|
int in_startAngle = (nOffset + 90) * 16;
|
int in_spanAngle = (60)*16;
|
painter.drawArc(in_rectangle,in_startAngle,in_spanAngle);
|
|
in_startAngle = (nOffset + 90+120) * 16;
|
in_spanAngle = (60)*16;
|
painter.drawArc(in_rectangle,in_startAngle,in_spanAngle);
|
|
|
in_startAngle = (nOffset + 90+240) * 16;
|
in_spanAngle = (60)*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);
|
|
|
QRectF sec_rectangle(wmargin+10, hmargin+10, s-20, s-20);
|
int sec_startAngle = 90 * 16;
|
int sec_spanAngle = ((position / range)*360)*16;
|
|
QConicalGradient gradient;
|
gradient.setCenter(sec_rectangle.center());
|
gradient.setAngle(90);
|
gradient.setColorAt(0, QColor(178, 255, 246));
|
gradient.setColorAt(1, QColor(5, 44, 50));
|
|
// int arcLengthApproximation = m_width + m_width / 3;
|
QPen pen2(QBrush(gradient), thick);
|
//pen.setCapStyle(Qt::RoundCap);
|
// painter.setPen(pen2);
|
painter.drawArc(sec_rectangle,sec_startAngle,sec_spanAngle);
|
|
|
QRect rect2(wmargin + 15, hmargin +20, s-30,s-40);
|
//绘制文本,绘制红色的文字
|
|
painter.setPen(QPen(QColor(255,0,0)));
|
//添加绘制的字体和字号
|
QString s1;
|
//s1=QString::number(int(position));
|
int nMinute = int(position) / 60;
|
int nSecond = int(position) % 60;
|
if (nMinute > 0) {s1=QString::number(nMinute)+":"+tr("%1").arg(nSecond,2,10,QChar('0'));}
|
else s1 = QString::number(nSecond);
|
|
int l = s1.length();
|
double fr = sqrt(l*l/2 + 4);
|
int newfontsize = (s-10)/fr;
|
if (newfontsize > fontsize) fontsize +=1;
|
if (newfontsize < fontsize) fontsize = newfontsize;
|
painter.setFont(QFont("Microsoft YaHei",fontsize));
|
// painter.drawText(this->rect(),s1);
|
painter.drawText(rect2,Qt::AlignCenter,s1);
|
|
}
|
|
void QMyTimeDisplay::SetRange(int n)
|
{
|
range=n;
|
}
|
void QMyTimeDisplay::SetPosition(float n)
|
{
|
position=n;
|
repaint();
|
}
|