Qt动态时钟 Qt实现简单动态时钟
weixin_45752304 人气:0任务实现:
通过Qt语言使用计时器实现一个简单的动态时钟;
实现效果:
实现过程:
整体过程主要分为两个部分:
1.绘制一个表盘所需的元素:时分秒针,刻度,还可以插入钟面背景图片;
2.让所绘制的指针动起来(使用计时器并关联系统时间);
整体代码:
dialog.h
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> QT_BEGIN_NAMESPACE namespace Ui { class Dialog; } QT_END_NAMESPACE class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = nullptr); ~Dialog(); void paintEvent(QPaintEvent *event); void drawSecond(QPainter *painter); void drawMinute(QPainter *painter); void drawHour(QPainter *painter); void drawClock(QPainter *painter); private: Ui::Dialog *ui; }; #endif // DIALOG_H
dialog.cpp
#include "dialog.h" #include "ui_dialog.h" #include <QPainter> #include <QTime> #include <QTimer> #include <QPixmap> #include <QPen> Dialog::Dialog(QWidget *parent) : QDialog(parent) , ui(new Ui::Dialog) { ui->setupUi(this); resize(959,959); setWindowTitle("My Clock"); QTimer *timer = new QTimer(this); timer->start(1000); //信号的链接 connect(timer,SIGNAL(timeout()),this,SLOT(update())); } Dialog::~Dialog() { delete ui; } void Dialog::paintEvent(QPaintEvent *event) { QPainter painter(this); QPixmap map(":/Resrouse/Pic.jpg"); QRect q(0,0,959,959); QRect q2(0,0,width(),height()); painter.drawPixmap(q2,map,q); //防止图形走样 painter.setRenderHint(QPainter::Antialiasing,true); //使得窗口可调节,图形随之变形 painter.setWindow(0,0,200,200); //重新设置坐标轴的原点位置 painter.translate(100,100); //调用绘制图像函数,分别绘制时分秒 drawClock(&painter); drawSecond(&painter); drawMinute(&painter); drawHour(&painter); } void Dialog::drawSecond(QPainter *painter) { static const QPoint Second[4]= { QPoint(2, 5), QPoint(0, 18), QPoint(-2, 5), QPoint(0, -90) }; //获取当前系统时间currentTime(); QTime time = QTime::currentTime(); //设置绘制颜色 painter->setBrush(Qt::red); painter->setPen(Qt::red); //save进行保存 painter->save(); //完成绘制(时间换算倾斜角度) painter->rotate(6.0*time.second()); painter->drawConvexPolygon(Second,4); //ratate进行复位 painter->restore(); } void Dialog::drawMinute(QPainter *painter) { static const QPoint Minute[4]= { QPoint(2, 5), QPoint(0, 16), QPoint(-2, 5), QPoint(0, -70) }; QTime time = QTime::currentTime(); painter->setBrush(Qt::blue); painter->setPen(Qt::blue); painter->save(); painter->rotate(6.0*(time.minute()+time.second()/60.0)); painter->drawConvexPolygon(Minute,4); painter->restore(); } void Dialog::drawHour(QPainter *painter) { static const QPoint Hour[4]= { QPoint(2, 5), QPoint(0, 13), QPoint(-2, 5), QPoint(0, -40) }; QTime time = QTime::currentTime(); painter->setBrush(Qt::yellow); painter->setPen(Qt::yellow); painter->save(); painter->rotate(30.0*(time.hour()+time.minute()/60.0)); painter->drawConvexPolygon(Hour,4); painter->restore(); } void Dialog::drawClock(QPainter *painter) { QPen pen; pen.setWidth(2); pen.setColor(Qt::white); painter->setPen(pen); //绘制钟表刻度盘和数字 for (int i = 1; i <=60; ++i) { painter->save(); painter->rotate(6*i);//坐标轴旋转6度 //分别绘制长短线 if (i % 5 == 0) { painter->drawLine(0, -98, 0, -82); painter->drawText(-20, -82, 40, 40,Qt::AlignHCenter | Qt::AlignTop,QString::number(i/5)); } else { painter->drawLine(0, -98, 0, -88); } painter->restore();//绘制图形后复位坐标系 } }
表盘绘制:
使用paintEvent()函数,以结构体形式来储存每一个指针的大小位置,并进行绘制;
下面用秒针的绘制作为例子:
void Dialog::drawSecond(QPainter *painter) { static const QPoint Second[4]= { QPoint(3, 5), QPoint(0, 18), QPoint(-3, 5), QPoint(0, -90) }; //获取当前系统时间currentTime(); QTime time = QTime::currentTime(); //设置绘制颜色 painter->setBrush(Qt::red); painter->setPen(Qt::red); //save进行保存 painter->save(); //完成绘制(时间换算倾斜角度) painter->rotate(6.0*time.second()); painter->drawConvexPolygon(Second,4); //ratate进行复位 painter->restore(); }
调用时间:
设置计时器的时间为1000ms,即1s;
QTimer *timer = new QTimer(this); timer->start(1000); //信号的链接 connect(timer,SIGNAL(timeout()),this,SLOT(update()));
其他:
1.图片的插入:
map中内容可替换为自行导入的资源文件
QPixmap map(":/Resrouse/Pic.jpg"); QRect q(0,0,959,959); QRect q2(0,0,width(),height()); painter.drawPixmap(q2,map,q);
2.绘制颜色的改变:
QPen pen; //设置画笔的宽度 pen.setWidth(2); //设置画笔的颜色 pen.setColor(Qt::white); //调用设置的画笔参数 painter->setPen(pen);
加载全部内容