在Windows系统上怎么用QT5实现一个时钟桌面

在Windows系统上怎么用QT5实现一个时钟桌面

这篇文章主要讲解了“在Windows系统上怎么用QT5实现一个时钟桌面”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“在Windows系统上怎么用QT5实现一个时钟桌面”吧!

介绍

这是一个简单的时钟运行界面,项目的结构如图所示,主要包含一个头文件:** analogclock.h **,两个源文件: ** analogclock.cpp main.cpp **.

实现代码

clock.pro

QT+=coreguigreaterThan(QT_MAJOR_VERSION,4):QT+=widgetsCONFIG+=c++11#YoucanmakeyourcodefailtocompileifitusesdeprecatedAPIs.#Inordertodoso,uncommentthefollowingline.#DEFINES+=QT_DISABLE_DEPRECATED_BEFORE=0x060000#disablesalltheAPIsdeprecatedbeforeQt6.0.0SOURCES+=\main.cpp\analogclock.cppHEADERS+=\analogclock.h#Defaultrulesfordeployment.qnx:target.path=/tmp/$${TARGET}/binelse:unix:!android:target.path=/opt/$${TARGET}/bin!isEmpty(target.path):INSTALLS+=target

analogclock.h

#ifndefANALOGCLOCK_H#defineANALOGCLOCK_H#includeclassAnalogClock:publicQWidget{Q_OBJECTpublic:AnalogClock(QWidget*parent=0);protected:voidpaintEvent(QPaintEvent*event)override;};#endif//WIDGET_H

analogclock.cpp

#include#include"analogclock.h"AnalogClock::AnalogClock(QWidget*parent):QWidget(parent){QTimer*timer=newQTimer(this);//实例一个QTimer的类connect(timer,SIGNAL(timeout()),this,SLOT(update()));//监控timeout()信号是否发出//timeout()表示:Thissignalisemittedwhenthetimertimesout.//指计时器发出信号,即下面的延时器发出信号timer->start(1000);//设置1s的延时/**forafunction*voidQTimer::start(intmsec)*Startsorrestartsthetimerwithatimeoutintervalofmsecmilliseconds.*Ifthetimerisalreadyrunning,itwillbestoppedandrestarted.*IfsingleShotistrue,thetimerwillbeactivatedonlyonce.*单位是毫秒,表示每一秒设置一个信号发出*/setWindowTitle(tr("AnalogClock"));//voidsetWindowTitle(constQString&)resize(200,200);//初始值大小}voidAnalogClock::paintEvent(QPaintEvent*){/***repaint()orupdate()wasinvoked,*thewidgetwasobscuredandhasnowbeenuncovered,or*manyotherreasons.***/staticconstQPointhourHand[3]={QPoint(7,8),QPoint(-7,8),QPoint(0,-40)};//用于绘制时针的三角形staticconstQPointminuteHand[3]={QPoint(7,8),QPoint(-7,8),QPoint(0,-60)};//用于绘制分针的三角形staticconstQPointsecondHand[3]={QPoint(7,8),QPoint(-7,8),QPoint(0,-90)};//用于绘制秒针的三角形QColorhourColor(127,0,127);QColorminuteColor(0,127,127,191);//QColor::QColor(intr,intg,intb,inta=255)a表示透明度QColorsecondColor(220,20,60,100);//为每一个图形绘制颜色及透明度intside=qMin(width(),height());//我认为这一句的作用在于找到最小标出,用于坐标系的绘制QTimetime=QTime::currentTime();qDebug()<

main.cpp

#include"analogclock.h"#includeintmain(intargc,char*argv[]){QApplicationa(argc,argv);AnalogClockw;w.show();returna.exec();}

编译打包

编译

一般编译过程采用的是debug版本,但是给其他用户使用最好是release版本,因此打包前需要切换到release版本重新编译一遍。

这样在项目文件夹中会有两种版本的exe执行程序。

打包

生成release版本的exe后,进入文件夹中,将release文件夹中的clock.exe复制到单独的文件夹中 ,我复制到myClock文件夹中。

在开始菜单中,选择下图红色的cmd。

进入到myClock文件夹中,输入 windeployqt clock.exe

打包完成后,在myClock文件夹中就可以看到各种.dll链接库文件,这是exe文件依赖的库文件,此时双击clock.exe就可以动态显示时钟了。

将该文件夹打包,就可以部署到其他的Windows系统上。

感谢各位的阅读,以上就是“在Windows系统上怎么用QT5实现一个时钟桌面”的内容了,经过本文的学习后,相信大家对在Windows系统上怎么用QT5实现一个时钟桌面这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是恰卡编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

发布于 2022-01-21 23:15:49
分享
海报
52
上一篇:Django+Nginx+uWSGI定时任务怎么实现 下一篇:MyBatis查询无记录时返回值报错怎么办
目录

    忘记密码?

    图形验证码