闹钟
头文件:
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QDebug>
#include <QMouseEvent> //鼠标时间
#include <QPaintEvent> //绘制时间
#include <QPainter> //画家时间
#include <QPixmap> //图形引擎类
#include <QTimerEvent> //定时器事件处理类
#include <QTime> //时间类
#include <QTimer>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic slots:void updateTime();public:Widget(QWidget *parent = nullptr);~Widget();//重写绘制事件处理函数void paintEvent(QPaintEvent *event) override;//重写定时器处理时间函数
// void timerEvent(QTimerEvent *e) override;private:Ui::Widget *ui;
// int timer_id;
};
#endif // WIDGET_H
源文件:
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);
// timer_id=this->startTimer(1000);QTimer *timer = new QTimer(this);connect(timer,&QTimer::timeout,this,&Widget::updateTime);timer->start(1000);}Widget::~Widget()
{delete ui;
}//绘制事件处理函数的定义
void Widget::paintEvent(QPaintEvent *event)
{Q_UNUSED(event);//实例化一个画家QPainter painter(this);//实例化一个画笔QPen pen; //使用无参构造pen.setColor(QColor("green"));pen.setStyle(Qt::SolidLine); //设置线性pen.setWidth(5);// pen.setBrush(QBrush(Qt::RadialGradientPattern));//将画笔给画家安排上painter.setPen(pen);//给画家偏移起始坐标点painter.translate(this->width()/2,this->height()/2);//给画家偏移坐标系painter.rotate(0); //选择坐标系painter.setBrush(QBrush(QColor("yellow"))); //给画家设置画刷//画个圆painter.drawEllipse(QPoint(0,0),300,300);// painter.drawLine(0,-300,0,-265);// for(int i=0;i<12;i++) //旋转坐标系一周,每次30度,回到原来的点// {// painter.drawLine(0,-300,0,-265); //画小时的直线// painter.rotate(30); //旋转坐标系// }for(int i=0;i<60;i++) //旋转坐标系一周,每次6度,回到原来的坐标系{if((i*6)%30==0){painter.drawLine(0,-300,0,-265); //画小时的直线painter.rotate(6); //旋转坐标系}else{painter.drawLine(0,-300,0,-285); //画分钟的直线painter.rotate(6); //旋转坐标系}}/***************绘制适量文字****************/painter.translate(-this->width()/2,-this->height()/2);pen.setColor(QColor("violet"));painter.setPen(pen);//给画家设置字体painter.setFont(QFont("隶书",40,2));//绘制矢量文字painter.drawText(this->rect(),Qt::AlignCenter,"一个小闹钟");//绘制时针int X=this->width()/2;int Y=this->height()/2;painter.save(); // 保存当前的绘制状态//获取当前时间QTime currentTime = QTime::currentTime();painter.translate(X,Y); // 将绘制的坐标原点移动到表盘的中心int hour = currentTime.hour(); // 获取当前的小时int minute = currentTime.minute(); // 获取当前的分钟double hourAngle = (hour%12 + minute/60.0)*30.0; // 计算时针的角度,考虑分钟对时针的影响painter.rotate(hourAngle); // 旋转坐标系,使时针指向正确的位置painter.setPen(QPen(Qt::black,5)); //设置笔painter.drawLine(0,0,0,-300*0.5); // 绘制时针,起点是坐标原点 (0, 0),终点是时针的末端位置painter.restore(); // 恢复之前保存的绘制状态,以便后续绘制/***********************************下面同理******************************///绘制分针painter.save();painter.translate(X,Y);double minuteAngle = minute*6.0;painter.rotate(minuteAngle);painter.setPen(QPen(Qt::red,3)); //设置笔painter.drawLine(0,0,0,-300*0.7);painter.restore();//绘制秒针painter.save();painter.translate(X,Y);int second = currentTime.second();double secondAngle = second * 6.0;painter.rotate(secondAngle);painter.setPen(QPen(Qt::blue,2)); //设置笔painter.drawLine(0,0,0,-300*0.9);painter.restore();}//定时器处理时间函数的定义
void Widget::updateTime()
{update();
}