news 2026/4/17 16:10:00

用QT绘制一个简单的时钟

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
用QT绘制一个简单的时钟

定义时钟类AnalogClock.h

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

实现时钟类

#include "analogclock.h" #include<QTimer> #include <QTime> #include <QPainter> AnalogClock::AnalogClock(QWidget *parent) : QWidget(parent) { QTimer *timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(update())); timer->start(1000); setWindowTitle(tr("Analog Clock")); resize(200,200); } AnalogClock::~AnalogClock() { } void AnalogClock::paintEvent(QPaintEvent *event) { 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,-70) }; QColor hourColor(127,0,127); QColor minuteColor(0,127,127,191); int side = qMin(width(),height()); QTime time = QTime::currentTime(); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.translate(width()/2,height()/2); painter.scale(side / 200.0,side / 200.0); painter.setPen(Qt::NoPen); painter.setBrush(hourColor); painter.save(); painter.rotate(30.0*((time.hour() + time.minute() / 60.0))); painter.drawConvexPolygon(hourHand,3); painter.restore(); 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); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/17 14:21:47

5步彻底解决Zotero文献库重复条目:智能合并插件完全指南

5步彻底解决Zotero文献库重复条目&#xff1a;智能合并插件完全指南 【免费下载链接】ZoteroDuplicatesMerger A zotero plugin to automatically merge duplicate items 项目地址: https://gitcode.com/gh_mirrors/zo/ZoteroDuplicatesMerger 还在为Zotero文献库中大量…

作者头像 李华
网站建设 2026/4/17 21:44:12

OneMore插件:打破OneNote限制的160+实用功能全解析

OneMore插件&#xff1a;打破OneNote限制的160实用功能全解析 【免费下载链接】OneMore A OneNote add-in with simple, yet powerful and useful features 项目地址: https://gitcode.com/gh_mirrors/on/OneMore 你是否曾经在使用OneNote时感到束手束脚&#xff1f;图片…

作者头像 李华
网站建设 2026/4/1 9:39:15

Mac滚动方向终极解决方案:Scroll Reverser让你的手腕不再抗议

Mac滚动方向终极解决方案&#xff1a;Scroll Reverser让你的手腕不再抗议 【免费下载链接】Scroll-Reverser Per-device scrolling prefs on macOS. 项目地址: https://gitcode.com/gh_mirrors/sc/Scroll-Reverser 你是否曾经在触控板上流畅地向下滑动&#xff0c;却在外…

作者头像 李华
网站建设 2026/4/18 5:38:20

Sunshine游戏串流终极解决方案:从设备限制到全平台畅玩

Sunshine游戏串流终极解决方案&#xff1a;从设备限制到全平台畅玩 【免费下载链接】Sunshine Sunshine: Sunshine是一个自托管的游戏流媒体服务器&#xff0c;支持通过Moonlight在各种设备上进行低延迟的游戏串流。 项目地址: https://gitcode.com/GitHub_Trending/su/Sunsh…

作者头像 李华
网站建设 2026/4/9 11:52:03

解决地址别名问题:MGeo实体对齐实战案例

解决地址别名问题&#xff1a;MGeo实体对齐实战案例 在城市计算、物流调度、地图服务等场景中&#xff0c;地址数据的标准化与实体对齐是构建高质量地理信息系统的前提。然而&#xff0c;现实中的地址表达存在大量“同地异名”现象——例如“北京市海淀区中关村大街1号”与“北…

作者头像 李华
网站建设 2026/4/15 20:44:46

3步搞定QQ空间历史说说完整备份神器GetQzonehistory

3步搞定QQ空间历史说说完整备份神器GetQzonehistory 【免费下载链接】GetQzonehistory 获取QQ空间发布的历史说说 项目地址: https://gitcode.com/GitHub_Trending/ge/GetQzonehistory 还在为那些承载着青春记忆的QQ空间说说无法批量保存而烦恼吗&#xff1f;GetQzonehi…

作者头像 李华