1 .h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPaintEvent> //绘制事件
#include <QPainter> //画家类
#include <QPaintDevice>
#include <cmath> //
#include <QPainterPath>
#include <QTime> //时间类
#include <QTimer> //定时器
#include <QTimerEvent> //定时器事件
#include <QDebug>
#include <QBrush> //画刷
#define PI 3.14159
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected:
void paintEvent(QPaintEvent *event) override; //重写绘制事件函数
void timerEvent(QTimerEvent *event) override; //重写定时器事件函数
void DrawScaleLine(QPainter& painter, QPen pain_pen); //绘制刻度线函数
void DrawSecLine(); //绘制秒针函数
void DrawMinuLine(); //绘制分针函数
void DrawHourLine(); //绘制时针函数
private:
Ui::Widget *ui;
QTimer* timer; //定时器指针
int sec=0, minu=0, hour=0; //用于存储时分秒
};
#endif // WIDGET_H
2 .CPP
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
timer = new QTimer(this);//实例化
QTime time;
sec = time.currentTime().second();//获取当前时间
minu = time.currentTime().minute();
hour = time.currentTime().hour();
//qDebug() << sec << " " << minu;
this->startTimer(1000);//启动定时器
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
//实例化一个画家类
QPainter pt(this);
pt.setRenderHint(QPainter::Antialiasing, true);//开启反锯齿
//设置画笔(RGB)
QPen pen(QColor(255,50,70));
pen.setWidth(8);//设置画笔宽度
pt.translate(this->width()/2,this->height()/2);//设置画笔起点
pt.setPen(pen);
//绘制圆
pt.drawEllipse(QPoint(0,0), 200, 200);
DrawScaleLine(pt, pen);//绘制刻度线
//实例化一个画家类
QPainter pt_1(this);
pt_1.setRenderHint(QPainter::Antialiasing, true);//开启反锯齿
//设置画笔(RGB)
QPen pen_1(QColor(0,0,0));
pen_1.setWidth(8);//设置画笔宽度
pt_1.translate(this->width()/2,this->height()/2);//设置画笔起点
pt_1.setPen(pen_1);
//绘制圆
pt_1.drawEllipse(QPoint(0,0), 8, 8);
DrawSecLine(); //秒针
DrawMinuLine(); //分针
DrawHourLine(); //时针
//让绘制事件不断的触发
this->update();
}
//绘制刻度线函数
void Widget::DrawScaleLine(QPainter& painter,QPen pain_pen)
{
//粗刻度线
for(int i=0; i<12; i++) {
pain_pen.setWidth(6);//宽度
pain_pen.setColor("black");
painter.setPen(pain_pen);
painter.drawLine(QPoint(0, 180), QPoint(0, 192));
painter.rotate(30);
}
//细刻度线,上下二者可%5操作(二合一for循环)
for(int i=0; i<60; i++) {
pain_pen.setWidth(3);
pain_pen.setColor("black");
painter.setPen(pain_pen);
painter.drawLine(QPoint(0,195), QPoint(0,190));
painter.rotate(6);
}
painter.end();
}
//绘制秒针函数
void Widget::DrawSecLine()
{
QPainter psec(this);
//设定初始位置
psec.translate(this->width()/2,this->height()/2);
//锯齿取消
psec.setRenderHint(QPainter::Antialiasing,true);
//设置每个时钟
//颜色
QPen sec_pen(QColor(0, 160, 230));
//秒针宽度
sec_pen.setWidth(2);
//绑定画家和笔
psec.setPen(sec_pen);
//秒针偏移量
psec.rotate(sec*6);
//设定秒针长度及初始位置
psec.drawLine(QPoint(0,0),QPoint(0,-150));
psec.end();
}
//绘制分针
void Widget::DrawMinuLine()
{
QPainter pmin(this);
//设定初始位置
pmin.translate(this->width()/2,this->height()/2);
//锯齿取消
pmin.setRenderHint(QPainter::Antialiasing,true);
//分针颜色
QPen min_pen(QColor(0, 127, 127));
//分针宽度
min_pen.setWidth(6);
//绑定画家和笔
pmin.setPen(min_pen);
//设定分针偏移量
pmin.rotate(minu*6+sec*0.1);
//设定分针长度及初始
pmin.drawLine(QPoint(0,0),QPoint(0,-120));
pmin.end();
}
//时针
void Widget::DrawHourLine()
{
QPainter phou(this);
//设定初始位置
phou.translate(this->width()/2,this->height()/2);
//锯齿取消
phou.setRenderHint(QPainter::Antialiasing,true);
//时针颜色
QPen hou_pen(QColor(200, 100, 0));
//秒针宽度
hou_pen.setWidth(10);
//绑定画家和笔
phou.setPen(hou_pen);
//偏移大小
phou.rotate(hour*30+minu*0.5);
//设定时针长度及初始位置
phou.drawLine(QPoint(0,0),QPoint(0,-80));
phou.end();
}
//处理定时器事件
void Widget::timerEvent(QTimerEvent *event)
{
sec++;
if((sec % 60) == 0)//60秒进一
{
sec = 0;
minu++;
if((minu % 60) == 0)//60分进一
{
minu = 0;
hour++;
if((hour%12) == 0)//0点
{
hour = 0;
}
}
}
}
3 展示
4 牛客网刷题