#include <QWidget> // QWideet,类是所有用户界面对象的基类
#include<QPaintEvent> // QPaintEivet,类包含绘制事件的事件参数。
#include <QPainter> // QPainter 类在小部件和其他绘画设备上执行低级绘画
#include <QPen> // QPen类定义了0Painter应该如何绘制开状的线条和轮廓。
#include <QBrush> // brus类定义了QPainter 绘制的形状的填充模式
#include <QtMath> // 这些函数是C或标准模板库中不可用的基本数学运算的部分方便定义
#include <QTimer> // QTimer, 类提供重夏和单次计时器。
#include <QDebug> // QDebug,类为调试信息提供输出流。
绘制表盘
首先我们要确定绘制表盘需要哪些参数,半径和Π?按照数学的逻辑来说确实没错,不过把它运用到计算机中还是需要有所改变才可以,表盘的制作需要用到void QPainter::drawEllipse(int x, int y, int width, int height)这个函数,这是一个过载功能。以给定的宽度和高度绘制由从 (x, y) 开始的矩形定义的椭圆。
绘制刻度盘
绘制刻度盘我们需要知道每次转动的角度是多少,时钟有12个大的刻度,每个大的刻度有5个小刻度,所有秒钟每秒转动的角度为360/60 = 6°。圆心所在竖线上方向为y轴正半轴,0°参考点为x轴正半轴,顺时针旋转。从12点处开始绘制,那么我们的基础仰角就是90° * 3 = 270°,接下来依次是276°、282°。
绘制指针
时钟的指针分为时针、分针和秒针,原理基本上一模一样, 时针我们将其看作是一个等腰三角形,底边在中心指针圆域中,顶点指向刻度,所以我们需要知道三个点的坐标才可以绘制出时针(三角形)。
流程
首先我们分别通过
QTime::currentTime.hour
QTime::currentTime.minute
QTime::currentTime.second
获取系统的时、分、秒然后再把值赋给Hs、Ms、Ss,如何实时更新时间呢?我们需要一个定时器QTimer值设为1000,表示每间隔1秒返回一次系统时间。
直接上代码!
头文件定义
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QtMath>
#include <QTimer>
#define Pai 3.14
class Widget : public QWidget{
Q_OBJECT
public:
Widget(QWidget parent = 0);
~Widget();
void paintEvent(QpaintEvent *); //重写绘制事件函数
void Init_Parameter(); //初始化参数函教
void Draw_Clock(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 Hs; //时
int Ms; //分
int Ss; //秒
};
#endif
.cpp文件
#include "widget.h"
#include <QTime>
#Include <QDebug>
Widget.Widget(Qwidget"parent)
:QWidgetlparent)
{
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(update()));//定时调用绘制事件函教
timer->start(1000);//开启定时器
widget::~Widget()
}
void Widget::paintEvent(QpaintEvent *)
{
QPainter painter(this);
paintersetRenderHint(QPainter::Antialiasing);//设置抗锯齿
Draw_Clock( &painter);
painter.drawEllipse(Center_pos,R_Edge,R_Edge);
}
void Widget::Draw_Clock(QPainter*painter)//绘制时钟(刻度盘、刻度值、指针)
{
lnit_Parameter();
Draw_Dial(painter);
Draw_Text(painter);
Draw_Pointer(painter);
}
void Widget::lnit_Parameter()
Center_pos = QPoint(this->width()/2,this->height()/2);
R_Edge = this->height()/2;
R_Inside = R_Edge-10;
R_Center= 15;
R_Pointer = 6;
QTime Time = QTime::currentTime();//获取当前系统时间
Hs=Time.hour();
Ms=Time.minute();
S-Time.second();
}
void Widget::Draw_Dial(QPainter *painter)
{
painter->save();
for(int Loop = O; Loop <= Div_Max * Div_Min;Loop++)
{
float Angle= BassAngle + (360 / (Div_Max * Div_Min))*Loop;
int R= R_Inside - 1;
int x_start = Center_pos.x()+ R * cos((Angle / 180) * Pai);
int y_start = Center_pos.y()+ R * sin((Angle / 180) * Pai);
if(Loop % Div_Min == O)//判断大小刻度
{
QPen pen(Qt::black);
pen.stWidth(2);
painter->setPen(pen);
R=R_lnside-20;
}
else
{
QPen pen(Qt::black);
pen.setWidth(2);
painter->setPen(pen);
R=R_Inside-15;
}
int x_end = Center_pos.x()+R * cos((Angle / 180) * Pai);
int y_end = Center_pos.y()+R * sin((Angle / 180) * Pai);
painter->drawlLinel(Qpoint(x_start,y_start),QPoint(x_end,y_end));
}
painter->restore();
}
void Widget::Draw_Text(QPainter *painter)
{
painter->save();
QPen qPen(Qt::black);
qPen.setwidt(2);
painter->setPen(qPen);
QFont qFont("楷体",14,QFont: Bold,false);
painter->setFont(qFont);
int Dial_Text = 12;
for(int Loop = 0;Loop < Div_Max;Loop++)
{
if(Dial_Text > 12)
Dial_Text = 1;
intR = R_Inside - 60;
float Angle = BaseAngle + (360 / Div_Max ) * Loop;
int x = Center_pos.x()+R * cos((Angle / 180) * Pai);
int y = Center_pos.y()+R * sin((Angle / 180) * Pai);
painter->drawText(QRect(x-20,y-20,80,80),Qstring::number(Dial_Text++));
}
painter->restore();
}
void Widget.::Draw_Pointer(QPaintsr*painter)
{
painter->save();
QBrush qBrush= QBrush/Qcolor(Qt::black));
painter->seBrush(qBrush);
QPen qPen(Qt:.black);
qPen setwidtb(2);
painter->setPen(qPen);
//绘制秒针
float Angle = BaseAngle + (360 / (Div_Max * Div_Min)) * Ss;
float RightAngle = Angle + 90;
float LeftAngle = Angle - 90;
intR = R_Inside-1;
int x_start = Center_pos.×() + R * cos((Angle / 180) * Pai);
int y_start = Center_pos.y() + R * sin((Angle / 180) * Pai);
R=R_Pointer-1;
int x_end1 = Center_pos.×() + R * cos(RightAngle * 3.14 / 180);
int y_end1 = Center_p0s.y() + R * sin(RightAngls * 3.14 / 180);
int x_end2 = Center_pos.x() + R * cos(LeftAnge * 3.14 / 180);
int y_end2 = Center_pos.y() + R * sin(LeftAngls * 3.14/ 180);
QPointF aTriangle_S[3] ={QPoint(x_start,y_start),QPoint(x_end1,y_end1),QPoint(x_end2,y_end2)};
painter->drawPolygon(qTriangle_s,3);
//绘制分针
Angle= BaseAngle+(360/ (Div_Max Div_Min))*Ms;RightAngle = Angle +90;
LeftAnge= Angle - 90;
R = R_Inside - 60;
x_start = Center_pos.×()+R * cos((Angle / 180)*Pa);
y_start = Center _pos.y()+R * sin((Angle / 180)*Pa);
R = R_Pointer-1;
x_end1 = Center_pos.×()+ R * cos(RightAngle* 3.14 / 180);
y_end1 = Center_pos.y()+ R * sin(RightAngle* 3.14 / 180);
x_end2 = Center_pos.×()+ R * cos(LeftAngle * 3.14 / 180);
y_end2 = Center_pos.y()+ R * sin(LeftAngle *3.14 / 180);
QPointF qTriangle_M[3]={QPoint(x_start,y_start),QPoint(x_end1,y_end1),QPoint(x_end2,y_end2)};
painter->drawPolygon(qTriangle_M,3);
//绘制时针
Angle = BassAngle+(360 / Div_Max) * Hs;
RightAngle = Angle + 90;
LeftAngle= Angle - 90;
R = R_Inside-120;
x_start = Center_pos.×()+R*cos((Angle / 180)*Pa);
y_start = Center_pos.y()+R*sin((Angle / 180)* Pai);
R=R_Pointer-1;
x_end1 = Center_pos.x() + R*cos(RightAngle * 3.14 / 180);
y_end1 = Center_pos.y() + R*sin(RightAngle * 3.14 / 180);
x_end2 = Center_pos.x() + R*cos(LeftAnele * 3.14 / 180);
y_end2 = Center_pos.y() + R*sin(LeftAngle *3.14/ 180);
QPointF qTriangle_H[3] = {QPoint(x_start,y_start),QPoint(x_end1,y_end1),QPoint(x_end2,y_end2);
painter->drawPolygon(qTriangle_H,3);
painter->restore();
}