新西兰服务器

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


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

发布时间:2022-01-21 16:54:52 来源:高防服务器网 阅读:63 作者:iii 栏目:开发技术

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

介绍

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

实现代码

clock.pro 

QT       += core gui    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets    CONFIG += c++11    # You can make your code fail to compile if it uses deprecated APIs.  # In order to do so, uncomment the following line.  #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0    SOURCES +=       main.cpp       analogclock.cpp    HEADERS +=       analogclock.h    # Default rules for deployment.  qnx: target.path = /tmp/$${TARGET}/bin  else: unix:!android: target.path = /opt/$${TARGET}/bin  !isEmpty(target.path): INSTALLS += target

analogclock.h 

#ifndef ANALOGCLOCK_H  #define ANALOGCLOCK_H    #include <QWidget>    class AnalogClock : public QWidget  {      Q_OBJECT    public:      AnalogClock(QWidget *parent=0);  protected:      void paintEvent(QPaintEvent *event) override;  };  #endif // WIDGET_H

analogclock.cpp

#include <QtWidgets>  #include "analogclock.h"  AnalogClock::AnalogClock(QWidget *parent)      : QWidget(parent)  {      QTimer *timer = new QTimer(this);      //实例一个QTimer的类      connect(timer, SIGNAL(timeout()), this, SLOT(update()));      //监控timeout()信号是否发出      //timeout()表示:This signal is emitted when the timer times out.      //指计时器发出信号,即下面的延时器发出信号      timer->start(1000);//设置1s的延时     /*      *for a function      * void QTimer::start(int msec)      * Starts or restarts the timer with a timeout interval of msec milliseconds.      * If the timer is already running, it will be stopped and restarted.      * If singleShot is true, the timer will be activated only once.      * 单位是毫秒,表示每一秒设置一个信号发出      */      setWindowTitle(tr("Analog Clock"));      //void setWindowTitle(const QString &)      resize(200, 200);      //初始值大小  }  void AnalogClock::paintEvent(QPaintEvent *)   {      /*       *       *   repaint() or update() was invoked,       *   the widget was obscured and has now been uncovered, or       *   many other reasons.       *       *       */      static const QPoint hourHand[3] = {          QPoint(7, 8),          QPoint(-7, 8),          QPoint(0, -40)      };//用于绘制时针的三角形      static const QPoint minuteHand[3] = {          QPoint(7, 8),          QPoint(-7, 8),          QPoint(0, -60)      };//用于绘制分针的三角形      static const QPoint secondHand[3]={          QPoint(7,8),          QPoint(-7,8),          QPoint(0,-90)      };//用于绘制秒针的三角形        QColor hourColor(127, 0, 127);      QColor minuteColor(0, 127, 127, 191);      //QColor::QColor(int r, int g, int b, int a = 255)a表示透明度      QColor secondColor(220,20,60,100);      //为每一个图形绘制颜色及透明度        int side = qMin(width(), height());      //我认为这一句的作用在于找到最小标出,用于坐标系的绘制        QTime time = QTime::currentTime();      qDebug()<<time<<'n';//用于检验现在的时间        QPainter painter(this);//Qt强大的画图工具      painter.setRenderHint(QPainter::Antialiasing);// 用于反锯齿      //针对所有的组件,都反锯齿//表示设置渲染提示        painter.translate(width() / 2, height() / 2);//将原点放在中心      painter.scale(side / 200.0, side / 200.0);//Scales the coordinate system by (sx, sy).标尺坐标系      //Qt画板的x和y表示什么,x表示横线吗,y表示纵线吗?对的      //说明横坐标的范围是-100到100      //   纵坐标的范围是-100到100    //时针:      painter.setPen(Qt::NoPen);//一般用于描边,Qt::NoPen表示画笔没有边界      painter.setBrush(hourColor);//一般用于填充        //先将原先的painter存储起来,对目前的painter操作,目前的操作不对原本的产生影响,即原本不旋转      painter.save();//首先将原先画笔类似于入栈,对另一个画笔操作      painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));//表示旋转,若缺少painter.save(),会对整个painter类旋转      painter.drawConvexPolygon(hourHand, 3);//绘制多边形      painter.restore();//与painter.save()配套使用          painter.setPen(hourColor);        for (int i = 0; i < 12; ++i) {          painter.drawLine(88, 0, 96, 0);          painter.rotate(30.0);//画横线,表示时间示数的标尺      }//分针和秒针同时针    //分针:      painter.setPen(Qt::NoPen);      painter.setBrush(minuteColor);        painter.save();      painter.rotate(6.0 * (time.minute() + time.second() / 60.0));      painter.drawConvexPolygon(minuteHand, 3);      painter.restore();        painter.setPen(minuteColor);      for (int j = 0; j < 60; ++j) {          if ((j % 5) != 0)              painter.drawLine(92, 0, 96, 0);          painter.rotate(6.0);      }    //时针:      painter.setPen(Qt::NoPen);      painter.setBrush(secondColor);        painter.save();      painter.rotate(6*time.second());      painter.drawConvexPolygon(secondHand,3);      painter.restore();      }

main.cpp

#include "analogclock.h"    #include <QApplication>    int main(int argc, char *argv[])  {      QApplication a(argc, argv);      AnalogClock w;      w.show();      return a.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实现一个时钟桌面这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是高防服务器网,小编将为大家推送更多相关知识点的文章,欢迎关注!

[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[