需求:
使用QT中的绘画工具与定时器工具实现简易时钟
代码实现过程:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QPainter> //引入画家类
#include<QPaintEvent> //引入绘制事件类
#include<QTime> //引入时间类
#include<QTimerEvent> //引入定时器事件类
#include<QTimer> //引入定时器类
#include<QDateTime> //引入日期时间类
#include<QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
//重写绘制事件函数
void paintEvent(QPaintEvent *event) override;
private slots:
void timeout_slot();
private:
Ui::Widget *ui;
QTimer* timer1; //定义一个定时器类的指针
quint8 hour;
quint8 minute;
quint8 second;
int cnt;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include<cmath>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(800,800);
this->cnt=0;
QTime timer = QTime::currentTime(); //获取时间
// QString t = timer.toString("HH:mm:ss A"); //转换为字符串
QString t = timer.toString("HH:mm:ss"); //转换为字符串
QStringList tlist = t.split(':'); //以:分割t
// for(int i=0;i<tlist.size();i++)
// qDebug() << tlist[i];
hour = 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)
{
// qDebug() << "***************"; //界面发生变化时调用
QPainter painter(this);
painter.setPen(QColor("blue")); //给画家准备画笔
painter.setFont(QFont("黑体",24));//设置字体
QBrush brush(QColor("pink")); //设置刷子——>设置填充色
painter.setBrush(brush);
QPen pen;
//画园
painter.translate(this->width()/2,this->height()/2);
painter.drawEllipse(QPoint(0,0),300,300);
// painter.drawEllipse(QPoint(400,400),350,350);
//画刻度 分
for(int i=0;i<60;i++){
pen.setColor(QColor("blue"));
pen.setWidth(1);
painter.setPen(pen);
painter.drawLine(QPoint(0,300),QPoint(0,290));
painter.rotate(6);
}
//画刻度 时
for(int i=0;i<12;i++){
pen.setColor(QColor("blue"));
pen.setWidth(3);
painter.setPen(pen);
painter.drawLine(QPoint(0,300),QPoint(0,280));
painter.rotate(30);
}
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,-30);
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,-60);
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,-90);
}
void Widget::timeout_slot(){
QTime timer = QTime::currentTime(); //获取时间
// QString t = timer.toString("HH:mm:ss A"); //转换为字符串
QString t = timer.toString("HH:mm:ss"); //转换为字符串
QStringList tlist = t.split(':'); //以:分割t
// for(int i=0;i<tlist.size();i++)
// qDebug() << tlist[i];
hour = tlist[0].toUInt();
minute = tlist[1].toUInt();
second = tlist[2].toUInt();
qDebug() << hour << ':' << minute << ':' << second;
update();
QPaintEvent* q = nullptr;
this->paintEvent(q);
this->cnt++;
qDebug()<<this->cnt;
}