1、使用Qt绘画事件处理画一个闹钟
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QDebug>
#include <QPaintEvent>//画画处理事件
#include <QPainter>//画画
#include <QTime> //时间类
#include <QTimer> //计时器类
#include <QIcon> //图标类
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void paintEvent(QPaintEvent *event);//绘制事件
public slots://timeout信号的槽函数void timeout_slot();private:Ui::Widget *ui;QPoint startpoin;//绘制起始点QTimer* timer1; //定义一个定时器类的指针quint8 hour;quint8 minute;quint8 second;int cnt;};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QtMath>
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);this->setFixedSize(800,800);//设置固定大小this->setWindowTitle("牛逼的闹钟");this->setWindowIcon(QIcon(":/icon/naozhong.png"));//设置窗体图标//给打开第一下获取时间QTime timer =QTime::currentTime();//获取当前时间QString t=timer.toString("hh:mm:ss");//转换为字符串QStringList tlist =t.split(":"); //以 :分割thour =tlist[0].toUInt(); //将小时传化为无符号整数minute =tlist[1].toUInt(); //将分钟传化为无符号整数second =tlist[2].toUInt(); //将秒传化为无符号整数timer1 = new QTimer(this);//给定时器类指针实例化对象timer1->start(1000);//将定时器类对象的timeout信号与自定义的槽函数进行连接connect(timer1,&QTimer::timeout,this,&Widget::timeout_slot);}Widget::~Widget()
{delete ui;
}
//绘制事件处理函数
void Widget::paintEvent(QPaintEvent *event)
{QPainter painter(this);//实例化一个画家painter.setRenderHint(QPainter::Antialiasing, true);//准备画笔QPen pen;pen.setColor(QColor("black"));//设置颜色//给画家安排上画笔painter.setPen(pen);//设置画家坐标起点painter.translate(this->width()/2,this->height()/2);QBrush brush1(QColor("skyblue"));//刷子painter.setBrush(brush1);//给大圆填充//绘制一个圆painter.drawEllipse(QPoint(0,0),300,300);QBrush brush(QColor("red"));//刷子painter.setBrush(brush);//给节点填充//给中心点画一个节点painter.drawEllipse(QPoint(0,0),10,10);//绘制小时刻度线for(int i=0;i<12;i++){pen.setColor(QColor("black"));pen.setWidth(3);painter.setPen(pen);painter.drawLine(QPoint(0,300),QPoint(0,280));//画一个长度20的直线painter.rotate(30);//旋转画家 30度}//绘制每五分钟刻度线for(int i=0;i<60;i++){pen.setColor(QColor("black"));pen.setWidth(1);painter.setPen(pen);painter.drawLine(QPoint(0,300),QPoint(0,290));//画一个长度10的直线painter.rotate(6);//旋转画家 6度}painter.setPen(QColor(100,100,100));QFont font;font.setFamily("SimHei");font.setPointSize(16);painter.setFont(font);//绘制数字 for(int i=0;i<12;++i){QPointF point(0,0);painter.save();//计算并移动绘图对象中心点point.setX(250*qCos(((60-i*30)*3.14)/180)); //我看不懂 你写就对了point.setY(250*qSin(((60-i*30)*3.14)/180)); //我看不懂 你写就对了//计算并移动绘图对象的中心点painter.translate(point.x(),-point.y());//绘制路径painter.drawText(-15, -15, 30, 30,Qt::AlignCenter,QString::number(i+1));//我看不懂 你写就对了painter.restore();}//绘制小时指针QPainter painterH(this);painterH.translate(this->width()/2,this->height()/2);pen.setColor(QColor("red"));pen.setWidth(6);painterH.setPen(pen);painterH.rotate(hour*30+(minute/60)*30+second/3600*30);painterH.drawLine(0,0,0,-90);//绘制分钟指针QPainter painterM(this);painterM.translate(this->width()/2,this->height()/2);pen.setColor(QColor("green"));pen.setWidth(4);painterM.setPen(pen);painterM.rotate(minute*6+second/60*6);painterM.drawLine(0,0,0,-120);//绘制秒指针QPainter painterS(this);painterS.translate(this->width()/2,this->height()/2);pen.setColor(QColor("blue"));pen.setWidth(2);painterS.setPen(pen);painterS.rotate(second*6);painterS.drawLine(0,0,0,-150);
}
//timeout信号的槽函数
void Widget::timeout_slot()
{QTime timer =QTime::currentTime();//获取当前时间QString t=timer.toString("hh:mm:ss");//转换为字符串QStringList tlist =t.split(":"); //以 :分割thour =tlist[0].toUInt(); //将小时传化为无符号整数minute =tlist[1].toUInt(); //将分钟传化为无符号整数second =tlist[2].toUInt(); //将秒传化为无符号整数update();QPaintEvent *Q=nullptr;this->paintEvent(Q);
}
效果图