#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include
#include
#include
#define PI 3.14
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void paintEvent(QPaintEvent *);//重写绘制事件函数
void Init_Parameter();//初始化参数函数
void Set_Mask(QPainter *);//设置窗体透明
void Draw_Clock(QPainter *);//绘制时钟
void Draw_Broad(QPainter *);//绘制时钟盘
void Draw_Dial(QPainter *);//绘制刻度盘
void Draw_Text(QPainter *);//绘制刻度值
void Draw_Pointer(QPainter *);//绘制指针
private:
QTimer *timer;//定时器
QPoint Center_pos;//时钟圆心坐标
int R_Edge;//外部圆半径
int R_Inside;//内部圆半径
int R_Center;//中心小圆半径
int R_Pointer;//中心指针圆半径
int Div_Max = 12;//大刻度
int Div_Min = 5;//小刻度
float BaseAngle = 270;
int H;//时
int M;//分
int S ;//秒
};
#endif // WIDGET_H
#include "dialog.h"
#include "ui_dialog.h"
#include
#include
#include
#include
#include
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
resize(959,959);
setWindowTitle("My Clock");
QTimer *timer = new QTimer(this);
timer->start(1000);
//信号的链接
connect(timer,SIGNAL(timeout()),this,SLOT(update()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPixmap map(":/Resrouse/Pic.jpg");
QRect q(0,0,959,959);
QRect q2(0,0,width(),height());
painter.drawPixmap(q2,map,q);
//防止图形走样
painter.setRenderHint(QPainter::Antialiasing,true);
//使得窗口可调节,图形随之变形
painter.setWindow(0,0,200,200);
//重新设置坐标轴的原点位置
painter.translate(100,100);
//调用绘制图像函数,分别绘制时分秒
drawClock(&painter);
drawSecond(&painter);
drawMinute(&painter);
drawHour(&painter);
}
void Dialog::drawSecond(QPainter *painter)
{
static const QPoint Second[4]=
{
QPoint(2, 5),
QPoint(0, 18),
QPoint(-2, 5),
QPoint(0, -90)
};
//获取当前系统时间currentTime();
QTime time = QTime::currentTime();
//设置绘制颜色
painter->setBrush(Qt::red);
painter->setPen(Qt::red);
//save进行保存
painter->save();
//完成绘制(时间换算倾斜角度)
painter->rotate(6.0*time.second());
painter->drawConvexPolygon(Second,4);
//ratate进行复位
painter->restore();
}
void Dialog::drawMinute(QPainter *painter)
{
static const QPoint Minute[4]=
{
QPoint(2, 5),
QPoint(0, 16),
QPoint(-2, 5),
QPoint(0, -70)
};
QTime time = QTime::currentTime();
painter->setBrush(Qt::blue);
painter->setPen(Qt::blue);
painter->save();
painter->rotate(6.0*(time.minute()+time.second()/60.0));
painter->drawConvexPolygon(Minute,4);
painter->restore();
}
void Dialog::drawHour(QPainter *painter)
{
static const QPoint Hour[4]=
{
QPoint(2, 5),
QPoint(0, 13),
QPoint(-2, 5),
QPoint(0, -40)
};
QTime time = QTime::currentTime();
painter->setBrush(Qt::yellow);
painter->setPen(Qt::yellow);
painter->save();
painter->rotate(30.0*(time.hour()+time.minute()/60.0));
painter->drawConvexPolygon(Hour,4);
painter->restore();
}
void Dialog::drawClock(QPainter *painter)
{
QPen pen;
pen.setWidth(2);
pen.setColor(Qt::white);
painter->setPen(pen);
//绘制钟表刻度盘和数字
for (int i = 1; i <=60; ++i)
{
painter->save();
painter->rotate(6*i);//坐标轴旋转6度
//分别绘制长短线
if (i % 5 == 0)
{
painter->drawLine(0, -98, 0, -82);
painter->drawText(-20, -82, 40, 40,Qt::AlignHCenter | Qt::AlignTop,QString::number(i/5));
}
else
{
painter->drawLine(0, -98, 0, -88);
}
painter->restore();//绘制图形后复位坐标系
}
}