QPainter - 八卦时钟

QPainter - 八卦时钟

上一篇我们在画时钟的时候,已经把基本的钟表指针和刻度都绘制过了

想要完成八卦时钟,就要绘制这个里面的八卦了。

先上个图:

在这里插入图片描述

有人和我说八卦不能转

再来一张图:

在这里插入图片描述

背景的绘制

我们需要删除之前所绘制的白色背景, 并且将背景修改为黄色

我这里是直接使用的styleSheet设置的背景色

setStyleSheet("background-color:yellow;");

太极八卦的绘制

这里面是三部分:

  • 太极的绘制

太极的绘制我们添加绘制太极的函数.

太极的阴阳鱼就是一个半圆添加一个圆和裁减一个圆,我们使用QPainterPath很容易实现这个。

之后我们在给添加的圆的中心再绘制一个颜色相反的小圆作为鱼眼,这样即可完成阴阳鱼的绘制

void Universe::drawUniverse(QPainter &painter)
{int radius = radius_ * 0.2;painter.save();painter.rotate(-degree_);// 绘制阳鱼painter.setPen(Qt::NoPen);QPainterPath circle, tmp;// 这个后面的两个参数时开始度数和移动过的度数circle.arcTo(-radius, -radius, radius * 2, radius * 2, 270, 180);tmp.addEllipse(QPointF(0, -radius / 2), radius / 2, radius / 2);circle -= tmp;tmp.clear();tmp.addEllipse(QPointF(0, radius / 2), radius / 2, radius / 2);circle += tmp;painter.fillPath(circle, Qt::white);tmp.clear();tmp.addEllipse(QPointF(0, radius / 2), radius / 4, radius / 4);painter.fillPath(tmp, Qt::black);// 绘制阴鱼,与阳鱼是一样的circle.clear();circle.arcTo(-radius, -radius, radius * 2, radius * 2, 90, 180);tmp.clear();tmp.addEllipse(QPointF(0, -radius / 2), radius / 2, radius / 2);circle += tmp;tmp.clear();tmp.addEllipse(QPointF(0, radius / 2), radius / 2, radius / 2);circle -= tmp;painter.fillPath(circle, Qt::black);tmp.clear();tmp.addEllipse(QPointF(0, -radius / 2), radius / 4, radius / 4);painter.fillPath(tmp, Qt::white);painter.restore();
}
  • 八卦的绘制

八卦的绘制是比阴阳鱼更简单的,八卦对应的是八个方位,我们按照方位给加上对应的卦象即可,我这里给了卦象顺序和描述

void Universe::drawBagua(QPainter &painter)
{// 因为每个卦象不一样,因此需要单独去绘制,这里写了8个int bottom = - radius_ * 0.25;int lineLength = 12;int midLength = 2;int interval = 4;painter.setPen(QPen(Qt::red, 2));painter.save();// painter.rotate(degree_);/* 乾三连 */QPoint point(-lineLength / 2, bottom);QPoint point2(lineLength / 2, bottom);painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval);point2 = QPoint(lineLength / 2,  bottom - interval);painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval * 2);point2 = QPoint(lineLength / 2,  bottom - interval * 2);painter.drawLine(point, point2);/* 巽下断 */painter.rotate(45);point = QPoint(-lineLength / 2, bottom);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom );painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom);point2 = QPoint(lineLength / 2,  bottom );painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval);point2 = QPoint(lineLength / 2, bottom - interval);painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval * 2);point2 = QPoint(lineLength / 2,  bottom - interval * 2);painter.drawLine(point, point2);/* 坎中满 */painter.rotate(45);point = QPoint(-lineLength / 2, bottom);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom );painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom);point2 = QPoint(lineLength / 2,  bottom );painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval);point2 = QPoint(lineLength / 2, bottom - interval);painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval * 2);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom - interval * 2);painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom - interval * 2);point2 = QPoint(lineLength / 2,  bottom - interval * 2);painter.drawLine(point, point2);/* 艮覆碗 */painter.rotate(45);point = QPoint(-lineLength / 2, bottom);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom );painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom);point2 = QPoint(lineLength / 2,  bottom );painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom - interval);painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom - interval);point2 = QPoint(lineLength / 2,  bottom - interval);painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval *2);point2 = QPoint(lineLength / 2, bottom - interval * 2);painter.drawLine(point, point2);/* 坤六断 */painter.rotate(45);point = QPoint(-lineLength / 2, bottom);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom );painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom);point2 = QPoint(lineLength / 2,  bottom );painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom - interval);painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom - interval);point2 = QPoint(lineLength / 2,  bottom - interval);painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval * 2);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom - interval * 2);painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom - interval * 2);point2 = QPoint(lineLength / 2,  bottom - interval * 2);painter.drawLine(point, point2);/* 震仰盂 */painter.rotate(45);point = QPoint(-lineLength / 2, bottom);point2 = QPoint(lineLength / 2,  bottom);painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom - interval);painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom - interval);point2 = QPoint(lineLength / 2,  bottom - interval);painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval * 2);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom - interval * 2);painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom - interval * 2);point2 = QPoint(lineLength / 2,  bottom - interval * 2);painter.drawLine(point, point2);/* 离中虚 */painter.rotate(45);point = QPoint(-lineLength / 2, bottom);point2 = QPoint(lineLength / 2,  bottom);painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom - interval);painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom - interval);point2 = QPoint(lineLength / 2,  bottom - interval);painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval * 2);point2 = QPoint(lineLength / 2,  bottom - interval * 2);painter.drawLine(point, point2);/* 兑上缼 */painter.rotate(45);point = QPoint(-lineLength / 2, bottom);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom);painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom);point2 = QPoint(lineLength / 2,  bottom);painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval);point2 = QPoint(-lineLength / 4 + midLength / 2,  bottom - interval);painter.drawLine(point, point2);point = QPoint(lineLength / 4 - midLength/2, bottom - interval);point2 = QPoint(lineLength / 2,  bottom - interval);painter.drawLine(point, point2);point = QPoint(-lineLength / 2, bottom - interval * 2);point2 = QPoint(lineLength / 2,  bottom - interval * 2);painter.drawLine(point, point2);painter.restore();
}
  • 外部文字的绘制

八卦外部的文字绘制不复杂,但是八角形坐标得算一下, 这里也是贴出代码,初中的三角函数知识,不做解释,如果不明白可以自己画画图

int radiusIn = 100 * 0.85;
int radiusOut = 100 * 1.15;
int radiusText = 100 * 0.5;
const QStringList list{"乾", "巽", "坎", "艮", "坤", "震", "离", "兑"};
double pointInX = qSin(M_PI /180.0 * 22.5) * radiusIn / 2;
double pointOutX = qSin(M_PI /180.0 * 22.5) * radiusOut / 2;
double pointInY = radiusIn / 2 * qCos(M_PI /180.0 * 22.5);
double pointOutY = radiusOut / 2 * qCos(M_PI /180.0 * 22.5);
void Universe::drawBaguaText(QPainter &painter)
{painter.save();// painter.rotate(degree_);painter.setPen(QPen(Qt::black, 1));for(int i = 0; i < 8; i++){painter.drawLine(-pointInX, pointInY, pointInX, pointInY);painter.drawLine(-pointOutX, pointOutY, pointOutX, pointOutY);painter.drawLine(pointInX, pointInY, pointOutX, pointOutY);painter.drawText(QRectF(-5, -radiusText, 10, 10), Qt::AlignCenter,  list[i]);painter.rotate(45);}painter.restore();update();
}

至此我们完成了所有的绘制部分,之前绘制钟表的背景部分的代码可以删除,我们只留下刻度和指针的代码,对了给指针的中心加一个白色的点来模拟固定的小钉子,不然看着有点小难受。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/62424.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

从零构建深度学习推理框架-4 框架中的算子注册机制

今天要讲的这一注册机制用到了设计模式中的工厂模式和单例模式&#xff0c;所以这节课也是对两大设计模式的一个合理应用和实践。KuiperInfer的注册表是一个map数据结构&#xff0c;维护了一组键值对&#xff0c;key是对应的OpType&#xff0c;用来查找对应的value&#xff0c;…

【Docker晋升记】No.1--- Docker工具核心组件构成(镜像、容器、仓库)及性能属性

文章目录 前言&#x1f31f;一、Docker工具&#x1f31f;二、Docker 引擎&#x1f30f;2.1.容器管理&#xff1a;&#x1f30f;2.2.镜像管理&#xff1a;&#x1f30f;2.3.资源管理&#xff1a;&#x1f30f;2.4.网络管理&#xff1a;&#x1f30f;2.5.存储管理&#xff1a;&am…

Android Ble蓝牙App(三)特性和属性

Ble蓝牙App&#xff08;三&#xff09;特性使用 前言正文一、获取属性列表二、属性适配器三、获取特性名称四、特性适配器五、加载特性六、显示特性和属性七、源码 前言 在上一篇中我们完成了连接和发现服务两个动作&#xff0c;那么再发现服务之后要做什么呢&#xff1f;发现服…

SpringBoot复习:(31)Controller中返回的对象是如何转换成json字符串给调用者的?

首先&#xff0c;SpringBoot自动装配了HttpMessageConvertersAutoConfiguration这个自动配置类 而这个自动配置类又通过Import注解导入了JacksonHttpMessageConvertersConfiguration类&#xff0c; 在这个类中配置了一个类型为MappingJackson2HttpMessageConverter类型的bean…

tomcat多实例与动静分离

实验&#xff1a;在一台虚拟机上配置多台tomcat 1.配置 tomcat 环境变量 vim /etc/profile.d/tomcat.sh source /etc/profile.d/tomcat.sh 2.修改 tomcat2 中的 server.xml 文件&#xff0c;要求各 tomcat 实例配置不能有重复的端口号 vim /usr/local/tomcat/tomcat2/conf/…

【逗老师的PMP学习笔记】11、项目风险管理

目录 一、规划风险管理1、【关键输出】风险管理计划2、【插一嘴】几种常见的分解结构 二、识别风险1、【关键工具】核对单2、【关键技术】SWOT分析3、【关键输出】风险登记册 三、实施定性风险分析1、【关键工具】概率和影响矩阵 四、实施定量风险分析1、【关键工具】数据分析1…

ucharts-地图

以唐山地图为例&#xff1a; 先去找需要的区域入下图&#xff0c;会得到一堆的经纬度&#xff0c;我把他它放到静态文件里&#xff0c;需要的是它的features <template><view class"charts-box"><qiun-data-charts type"map":opts"o…

腾讯云服务器远程连接的方法大全

腾讯云服务器怎么连接登录&#xff1f;腾讯云服务器支持多种远程连接方法&#xff0c;可以使用腾讯云管理控制台自带的远程连接工具&#xff0c;也可以使用第三方远程连接工具&#xff0c;如如PuTTY、Xshell等&#xff0c;Linux操作系统可以SSH登录&#xff0c;Windows可以使用…

【猿灰灰赠书活动 - 01期】- 【Python网络爬虫入门到实战】

说明&#xff1a;博文为大家争取福利&#xff0c;与机械工业出版社合作进行送书活动 图书&#xff1a;《Python网络爬虫入门到实战》 一、好书推荐 图书介绍 本书介绍了Python3网络爬虫的常见技术。首先介绍了网页的基础知识&#xff0c;然后介绍了urllib、Requests请求库以及X…

Vc - Qt - 绘制窗口背景色

要在Qt中绘制一个背景颜色&#xff0c;你可以使用Qt的绘图功能来完成。下面是一种简单的方法&#xff1a; 步骤1&#xff1a;在你想要绘制背景颜色的QWidget&#xff08;例如QMainWindow或QDialog&#xff09;的派生类中&#xff0c;重写 它的paintEvent函数。步骤2&#xff1a…

游戏行业实战案例 4 :在线时长分析

【面试题】某游戏数据后台设有「登录日志」和「登出日志」两张表。 「登录日志」记录各玩家的登录时间和登录时的角色等级。 「登出日志」记录各玩家的登出时间和登出时的角色等级。 其中&#xff0c;「角色id」字段唯一识别玩家。 游戏开服前两天&#xff08; 2022-08-13 至 …

docker镜像push到仓库

镜像可以很方便直接 push 到 docker 的公共仓库或阿里云仓库 一、Dockerpush指定仓库是什么&#xff1f; Dockerpush是Docker的一个命令&#xff0c;用于将本地的Docker镜像推送到Docker官方公共仓库或用户私人仓库。而指定仓库则是将这个Docker镜像推送到指定的仓库中。 通过D…