使用Qt创建模拟时钟

2023-11-02

main.cpp

  1. #include <QtGui/QApplication>  
  2. #include "analogclock.h"  
  3.   
  4. Q_DECL_EXPORT int main(int argc, char *argv[])  
  5. {  
  6.     QApplication theApp(argc, argv);  
  7.   
  8.     AnalogClock clock;  
  9.     clock.show();  
  10.   
  11.     return theApp.exec();  
  12. }  

analogclock.h

  1. #ifndef ANALOGCLOCK_H  
  2. #define ANALOGCLOCK_H  
  3.   
  4. #include <QWidget>  
  5.   
  6. class AnalogClock : public QWidget  
  7. {  
  8.     Q_OBJECT  
  9. public:  
  10.     explicit AnalogClock(QWidget *parent = 0);  
  11.   
  12. signals:  
  13.   
  14. public slots:  
  15.   
  16. protected:  
  17.      void paintEvent(QPaintEvent *event);  
  18. };  
  19.   
  20. #endif // ANALOGCLOCK_H void paintEvent(QPaintEvent *event);  


analogclock.cpp


  1. #include <QtGui>  
  2. #include "analogclock.h"  
  3.   
  4. AnalogClock::AnalogClock(QWidget *parent) :  
  5.     QWidget(parent)  
  6. {  
  7.     QTimer* timer = new QTimer(this);  
  8.     connect(timer, SIGNAL(timeout()), this, SLOT(update()));  
  9.     timer->start(1000);  
  10.     setWindowTitle(tr("Analog Clock"));  
  11.     resize(200, 200);  
  12.   
  13. }  
  14.   
  15. void AnalogClock::paintEvent(QPaintEvent *)  
  16. {  
  17.     // 时针和分针的点坐标  
  18.     //画的是三角形,所以需要三个点的坐标  
  19.     static const QPoint hourHand[3] = {  
  20.         QPoint(7, 8),  
  21.         QPoint(-7, 8),  
  22.         QPoint(0, -30)  
  23.     };  
  24.     static const QPoint minuteHand[3] = {  
  25.         QPoint(7, 8),  
  26.         QPoint(-7, 8),  
  27.         QPoint(0, -60)  
  28.     };  
  29.   
  30.     static const QPoint secondHand[3] = {  
  31.         QPoint(4, 8),  
  32.         QPoint(-4, 8),  
  33.         QPoint(0, -100)  
  34.     };  
  35.   
  36.     /* 设置时针和分针的颜色 */  
  37.     QColor hourColor(127, 0, 127);  
  38.     QColor minuteColor(0, 127, 127, 191);  
  39.     QColor secondColor(0, 100, 100);  
  40.   
  41.     /* QT自带函数,返回两个数的最小值 */  
  42.     int side = qMin(width(), height());  
  43.   
  44.     /* 获取当前时间 */  
  45.     QTime time = QTime::currentTime();  
  46.   
  47.     /* 
  48.         在this这个可绘画设备上创建一个绘画者 
  49.         设置渲染,用反锯齿效果 
  50.         用一个重载函数转换一下坐标系 
  51.         缩放一下这个坐标系 
  52.     */  
  53.     //The QPainter class performs low-level painting on widgets and other paint devices.  
  54.     QPainter painter(this);  
  55.   
  56.     //Sets the given render hint on the painter if on is true; otherwise clears the render hint  
  57.     //Renderhints are used to specify flags to QPainter that may or may not be respected by any given engine.  
  58.     painter.setRenderHint(QPainter::Antialiasing);  
  59.   
  60.     //Translates the coordinate system by the given offset  
  61.     painter.translate(width()/2, height()/2);  
  62.   
  63.     //Scales the coordinate system by (sx, sy).  
  64.     painter.scale(side/200.0, side/200.0);  
  65.   
  66.     /* 用一个重载函数设置一下画笔的类型,NoPen就是没有线 
  67.        再用一个重载函数设置一下笔刷的颜色,用默认类型 
  68.     */  
  69.     //Sets the painter's pen to be the given pen.  
  70.     //The pen defines how to draw lines and outlines, and it also defines the text color.  
  71.     painter.setPen(Qt::NoPen);  
  72.   
  73.     //sets the painter's brush to the given brush.  
  74.     //The painter's brush defines how shapes are filled.  
  75.     painter.setBrush(hourColor);  
  76.   
  77.     //Saves the current painter state (pushes the state onto a stack).  
  78.     //A save() must be followed by a corresponding restore(); the end() function unwinds the stack.  
  79.     painter.save(); //保存当前的绘画者状态  
  80.   
  81.     //用给出的角度旋转坐标系  
  82.     //Rotates the coordinate system the given angle clockwise.  
  83.     painter.rotate(30.0*((time.hour()+time.minute()/60.0)));  
  84.   
  85.     //用线把点连起来  
  86.     //Draws the convex polygon defined by the first pointCount points in the array points using the current pen.  
  87.     //The first point is implicitly connected to the last point, and the polygon is filled with the current brush().  
  88.     painter.drawConvexPolygon(hourHand, 3);  
  89.   
  90.     //恢复当前的绘画者状态  
  91.     //Restores the current painter state (pops a saved state off the stack).  
  92.     painter.restore();  
  93.   
  94.     painter.setPen(hourColor);  
  95.   
  96.     /* 画代表小时的线 */  
  97.     for (int i = 0; i < 12; ++i) {  
  98.         //Draws a line from (x1, y1) to (x2, y2) and sets the current pen position to (x2, y2).  
  99.         painter.drawLine(88, 0, 96, 0);  
  100.         painter.rotate(30.0);  
  101.     }  
  102.   
  103.     //下面画分钟的过程是上面的重复,只是分针的颜色用到了透明度  
  104.     painter.setPen(Qt::NoPen);  
  105.     painter.setBrush(minuteColor);  
  106.     painter.save();  
  107.     painter.rotate(6.0*(time.minute()+time.second()/60.0));  
  108.     painter.drawConvexPolygon(minuteHand, 3);  
  109.     painter.restore();  
  110.     painter.setPen(minuteColor);  
  111.   
  112.     /* 画代表分钟的线 */  
  113.     for (int j = 0; j < 60; ++j) {  
  114.         if((j % 5) != 0) {  
  115.             painter.drawLine(92, 0, 96,0);  
  116.         }  
  117.         painter.rotate(6.0);  
  118.     }  
  119.   
  120.     //下面画秒针的过程是上面的重复  
  121.     painter.setPen(Qt::NoPen);  
  122.     painter.setBrush(secondColor);  
  123.     painter.save();  
  124.     painter.rotate(6.0*(time.second()));  
  125.     painter.drawConvexPolygon(secondHand, 3);  
  126.     painter.restore();  
  127.     painter.setPen(minuteColor);  
  128. }  



FROM: http://www.cnblogs.com/johnpher/archive/2012/01/01/2570587.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用Qt创建模拟时钟 的相关文章

  • ES集群性能优化及维护

    ES集群性能优化及维护 注 集群 elasticsearch 版本为 v7 2 1 1 ES索引刷新间隔设置 index refresh interval 刷新时间 默认1 PUT index all settings preserve e
  • 基于C++的栈的两种实现(数组和链表)

    栈 概述 基本操作 用数组实现栈 用链表实现栈 测试 概述 栈是一种只能在表的顶端进行插入和删除运算的线性表 其主要特点是后进先出 LIFO 或先进后出 FILO 该数据结构的示意图如下 基本操作 函数名 用途 bool empty 判断栈
  • webpack4-react-babel7-antd框架的babelrc文件配置写法

    babelrc文件配置写法 webpack2 babel6的babelrc文件配置 presets env modules false stage 0 stage 2 react plugins react hot loader babel
  • sklean中transform和fit_transform区别

    sklean中transform和fit transform区别 在学习过程中看到在有的代码里 from sklearn preprocessing import StandardScaler sc StandardScaler x tra
  • JWT --- 入门学习

    不知道为什么 不用springboot test测试或者启动类启动 会报这个错误 找不到类路径 1 常见的认证机制 basic auth 每次请求都会携带用户的username password 易被黑客拦截 Cookie auth 我们请
  • Debian10开启路由转发以及配置dhcp中继

    文章目录 1 所需设备 2 任务描述 3 服务搭建 1 所需设备 Debian10需要两块网卡 网卡1 192 168 1 1 24 网卡2 10 1 1 1 24 2 任务描述 Debian10开启路由转发之后才可以启用用dhcp中继 允
  • SQL Server创建数据库和表

    本人第一次写博客 没有什么经验 请多多体谅 文章目录 SQL Server数据库的基础学习1 一 认识数据库 二 创建数据库 三 创建表 SQL Server数据库的基础学习1 一 认识数据库 1 数据库的基本概念 数据库 DataBase

随机推荐

  • 如何通过name获取单选框和复选框选中状态的value值?

    概述 有时候我们会遇到这组情况 就是需要通过单选框的name值获取到当前选中状态的value值 提交到后端 进行数据的修改 那么我们就来看看如何进行获取吧 应用场景 我们有时候需要获取到单选框或者是复选框选中的那个 得到它的value值 传
  • python面对对象实验_Python面向对象实现方法总结

    总结 类的定义 很久以前 语言都是面向过程的 经过计算机科学家的探索 出现了面向对象 面向对象可以解释生活中很多东西 比如人 人就是个对象 有参数 比如器官 身高啥的 有方法 比如跑步 学习等 不扯那么多了 对象就是类 在python中用c
  • Tomcat 配置错误界面

    Tomcat发生错误时跳转到错误页面 注意 5 0下操作需要删除掉注释语句 不然报错 原因未知 一 修改 tomcat 的配置文件 修改 tomcat 的配置文件 当页面发生错误时跳转到指定的页面 在 tomcat 中 web xml 文件
  • 某MR-SDK 手机类型摄像机切换后的脚本切换/添加组件/删除组件

    解决问题 因为该SDK已经自动会识别用户手机类型 因为我需要为摄像机添加OutlineEffect这个脚本 以实现高亮显示 该脚本要求一次只能添加在一个摄像机上 简单写个脚本 using System Collections using S
  • 源码环境下添加系统Service流程

    关于系统服务的分析 以及如何实现添加系统服务 分析详细跳转链接 Android系统服务 SystemService 简介 添加系统Service涉及的文件 修改文件 Android mk api current txt api system
  • C语言带参数的main函数

    在我们刚接触C语言的时候 我们所写的main主函数都是不带参数的 但是的实际开发应用中 大多数情况 带参数的main函数用的最多 不带参数的main函数 int main 实际上是int main void 带参数的main函数 int m
  • [4G&5G专题-27]:架构-UE终端的4G+5G双连接详解

    目录 第1章 什么是多连接 1 1 多连接概述 1 2 多连接的聚合和分离点的分类 1 3 多连接好处 1 4 双连接的本质 1 5 多连接的控制面与数据面连接方法分类 1 6 1C 2U模式下数据承载的三种方式 1 7 分清各种场景的基本
  • 13个你可能未使用过的Python教程!

    Python 是顶级编程语言之一 它具有许多程序员从未使用过的许多隐藏功能 在这篇博客中 我将分享你可能从未使用过的13 个 Python 特性 列表Stepping 这是一个 step 参数 可以通过采取几个步骤来分割你的列表 此外 你可
  • Mybatis-多表联查

    多表联查 一 步骤一 创建pojo实体类 二 步骤二 明确两个实体类之间的关系 三 步骤三 修改pojo实体类 四 步骤四 编写Mapper接口 五 步骤五 编写Mapper映射文件 题目1 通过订单id查询订单详情以及所属用户 题目2 通
  • mysql字段更新拼接_更新数据库中值为拼接字符串的字段

    我们开发系统涉及权限的时候 会处理到用户和角色的关系 通常情况下 我们会建一个用户角色关系映射表 user role mapping 字段有id user id role id 如果某个用户有多个角色 那么在user role mappin
  • C语言课程设计学生籍贯信息,C语言课程设计 学生籍贯信息记录簿设计.doc

    C语言与程序设计课程设计 学生籍贯信息记录簿设计 学 院 信息工程 班 级 物联1301班 学 号 131408119 姓 名 滕玲 一 设计目的 该软件主要是编辑一个学生籍贯信息记录簿记录每个学生信息 包括 学号 姓名 籍贯 具体功能 1
  • flex布局采用justify-content:space-between时,当为两个内容时中间被空出的解决方案

    我们在用flex布局的时候有时会用到justify content space between属性 这个属性是让弹性容器内的元素向两端对齐 div class box div div div div div div div div div
  • 广度优先遍历进阶

    第七周 BFS 广度优先搜索 733 127 130 317 505 529 1263 1197 815 934 广度优先模板 void bfs queue
  • filetime,systemtime相互转化,获取文件创建时间,访问时间,修改时间,获取指定时间之前之后的SYSTEMTIME

    deleteOldFiles cpp 定义控制台应用程序的入口点 include stdafx h include
  • 脑科学和类脑智能技术综述学习笔记

    文章目录 Part1 脑科学 1 脑科学与类脑研究概述 摘要 引言 1 国际脑科学和类脑研究的回顾与前瞻 1 1 脑科学的回顾 现代神经科学的起点是 神经解剖学和组织学 对神经系统结构的认识和分析 1 2 脑科学领域的重大问题 从图谱制作到
  • Unity3d实现红外热成像效果

    1 将需要在红外图像中高亮的物体设置到图层PostProcessing 2 新建一个相机CameraHighLight 设置其Culling Mask为PostProcessing 也就是在这个相机中只有PostProcessing图层的物
  • ArcGIS Pro免费试用申请与安装配置

    ArcGIS Pro免费试用申请与安装配置 每个邮箱可以申请21天的试用 1 打开申请网站 https www esri com zh cn arcgis products arcgis pro trial 2 注册ArcGIS试用 3 在
  • JS子窗口向父窗口传值

    方法一 用模式窗口 returnValue是javascript中html的window对象的属性 目的是返回窗口值 当用window showModalDialog函数打开一个IE的模式窗口 模式窗口就是子窗口 打开后不能操作父窗口 只能
  • 斐波那契数列求和——C语言(小白版)

    斐波那契数列求和 C语言 小白版 题目要求 斐波那契数列 1 1 2 3 5 8 13 21 34 不难发现当n gt 2时 an an 1 an 2 要求 当屏幕输入n n gt 2 时 输出前n项以及前n项的和 注意 我们不使用递归 也
  • 使用Qt创建模拟时钟

    main cpp include