#include<QRectF>
#include<QTimer>
#include<QTime>
#include<QPainter>
#include<QPoint>
#include<QColor>
#include<QFont>
#include<QtMath>
#include "clock.h"
clock::clock(QWidget *parent)
: QWidget(parent)
{
// The QTimer class provides a high-level programming interface for timers.
// To use it, create a QTimer, connect its timeout() signal to the appropriate slots, and call start().
// From then on, it will emit the timeout() signal at constant intervals.
timer1=new QTimer(this);
connect(timer1,SIGNAL(timeout()),this,SLOT(newtime()));
timer1->start(1000);
timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(update()));
timer->start(1000);
setWindowTitle(tr("clock"));
state=0;
hour=0;
minute=0;
second=0;
// Constructs a size with the given width and height.
resize(300,300);
}
clock::~clock()
{
}
void clock::paintEvent(QPaintEvent *)
{
static const QPoint hourHand[3]={
QPoint(3,8),
QPoint(-3,8),
QPoint(0,-40)
};
static const QPoint minuteHand[3]={
QPoint(3,8),
QPoint(-3,8),
QPoint(0,-70)
};
static const QPoint secondHand[3]={
QPoint(3,8),
QPoint(-3,8),
QPoint(0,-90)
};
// QColor::QColor(int r, int g, int b, int a = 255)
// Constructs a color with the RGB value r, g, b,(red,green,blue) and the alpha-channel (transparency)透明化 value of a.
// The color is left invalid if any of the arguments are invalid.
QColor hourcolor(127,0,127);
QColor minuteColor(0,127,127,191);
QColor secondColor(127,127,0,130);
int side=qMin(width(),height());
QTime time=QTime::currentTime();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// void QPainter::translate(qreal dx, qreal dy)
// This is an overloaded function.
// Translates the coordinate system by the vector (dx, dy).
painter.translate(width()/2,height()/2);
painter.scale(side/200.0,side/200.0);
//hour
painter.setPen(Qt::NoPen);
// void QPainter::setBrush(Qt::BrushStyle style)
// This is an overloaded function.
// Sets the painter's brush to black color and the specified style.
painter.setBrush(hourcolor);
painter.save();
if(state==0)
painter.rotate(30.0*(time.hour()+(time.minute()/60.0)));
else
painter.rotate(30.0*(hour+minute/60.0));
painter.drawConvexPolygon(hourHand,3);
//恢复到save()
painter.restore();
painter.setPen(hourcolor);
for(int i=0;i<12;++i)
{
painter.drawLine(88,0,96,0);
painter.rotate(30.0);
}
//绘制小时文本
int radius=100;
QFont font=painter.font();
font.setBold(true);
painter.setFont(font);
int pointSize=font.pointSize();
int nHour=0;
for(int i=0;i<12;++i)
{
nHour=i+3;
if(nHour>12)
nHour-=12;
painter.drawText(textRectF(radius*0.8,pointSize,i*30),Qt::AlignCenter,QString::number(nHour));
}
//minute
painter.setPen(Qt::NoPen);
painter.setBrush(minuteColor);
painter.save();
if(state==0)
painter.rotate(6*(time.minute()+time.second()/60.0));
else
painter.rotate(6*(minute+second/60.0));
painter.drawConvexPolygon(minuteHand,3);
painter.restore();
painter.setPen(minuteColor);
for(int j=0;j<60;++j)
{
if(j%5!=0)
painter.drawLine(92,0,96,0);
painter.rotate(6.0);
}
//second
painter.setPen(Qt::NoPen);
painter.setBrush(secondColor);
painter.save();
if(state==0)
painter.rotate(6*time.second());
else
painter.rotate(6*second);
painter.drawConvexPolygon(secondHand,3);
painter.restore();
// painter.setPen(secondColor);
// for(int j=0;j<60;++j)
// {
// painter.drawLine(92,0,96,0);
// painter.rotate(6);
// }
}
QRectF clock::textRectF(double radius,int pointSize,double angle)
{
QRectF rectF;
rectF.setX(radius*qCos(angle*M_PI/180.0) - pointSize*2);
rectF.setY(radius*qSin(angle*M_PI/180.0) - pointSize/2.0);
rectF.setWidth(pointSize*4);
rectF.setHeight(pointSize);
return rectF;
}
void clock::newtime()
{
if(state==1){
++second;
if(second==60)
{
++minute;
second=0;
} if(minute==60){
++hour;
minute=0;
}
if(hour>12)
hour-=12;
}}