QPainter - 八卦时钟

2024-03-24 06:50
文章标签 时钟 八卦 qpainter

本文主要是介绍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();
}

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

这篇关于QPainter - 八卦时钟的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/840773

相关文章

第10章 中断和动态时钟显示

第10章 中断和动态时钟显示 从本章开始,按照书籍的划分,第10章开始就进入保护模式(Protected Mode)部分了,感觉从这里开始难度突然就增加了。 书中介绍了为什么有中断(Interrupt)的设计,中断的几种方式:外部硬件中断、内部中断和软中断。通过中断做了一个会走的时钟和屏幕上输入字符的程序。 我自己理解中断的一些作用: 为了更好的利用处理器的性能。协同快速和慢速设备一起工作

油表加速器时钟屏保 就是这么酷酷的

太酷了!这个时钟屏保太有个性了!油表加速器时钟屏保,模拟汽车加速表盘,非常有质感,用它做屏保,简直就是酷毙了~!今天小编给大家分享一个非常实用好看的时钟屏保(芝麻时钟),官网地址(https://clock.zhimasoft.cn/?bili) 芝麻时钟-油表加速器时钟屏保 我们一起来看下,油表加速器表盘时钟屏保是怎么设置的? 1、打开芝麻时钟软件,找到"时钟屏保"菜单,点击后打开;(

彩虹数字屏保时钟 芝麻时钟开启个性化的时代 屏保怎么能少它

彩虹数字屏保时钟 芝麻时钟开启个性化的时代 屏保怎么能少它?电脑屏保多样化,让大家有了更多的选择,让更多人有机会把自己的电脑打扮得漂漂亮亮,今天小编给大家推荐:芝麻时钟(官网下载地址:http://clock.zhimasoft.cn/?bili),一起看下怎么开启彩虹数字时钟屏保吧! 彩虹数字屏保时钟 1、先下载芝麻时钟到电脑,可以百度,或者微软应用商城搜索“芝麻时钟”下载;

STM32CubeMX 1 创建一个新工程 利用时钟点亮LED KEIL5 Jlink配置

直接上ST的官网下载STM32CubeMX安装 地址: 单片机:STM32F103C8T6 带外部8MHz晶振 目的:利用Timer和使LED按照1Hz的频率闪烁。 在此方面学霸级人物的指引下学习了,并写此文章记录,以防忘记。 新建工程 出现如下界面,中央就是这个封装的引脚图: 接下来开始配置 1. 设置外部晶振接口在PD0和PD1 单机想要配置的引脚,出现选择菜单。

Android AnalogClock TextClock DigitalClock Chronometer 时钟 定时器

AnalogClock 相关属性:  android:dial="@drawable/img1" //表盘android:hand_hour="@drawable/alert_dialog_icon" //时针android:hand_minute="@drawable/alert_dialog_icon" //分针 TextClock 相关属性: android:f

RP2040 C SDK clocks时钟源配置使用

RP2040 C SDK clocks时钟源配置使用 🌿RP2040时钟源API函数文档:https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#group_hardware_clocks 🍁RP2040时钟树: 系统时钟源可以来自外部时钟输入(external clocks)、 晶体振荡器(XOS

分布式系统理论基础三-时间、时钟和事件顺序

GitHub:https://github.com/wangzhiwubigdata/God-Of-BigData 关注公众号,内推,面试,资源下载,关注更多大数据技术~大数据成神之路~预计更新500+篇文章,已经更新50+篇~ 现实生活中时间是很重要的概念,时间可以记录事情发生的时刻、比较事情发生的先后顺序。分布式系统的一些场景也需要记录和比较不同

Linux学习笔记12---主频和时钟配置实验

本章学习 I.MX6U 的时钟系统,学习如何配置 I.MX6U 的 系统时钟和其他的外设时钟,使其工作频率为 528MHz ,其他的外设时钟源都工作在 NXP 推荐的频率。  1、MX6U 时钟系统详解          I.MX6U 的系统主频为 528MHz,有些型号可以跑到 696MHz,但是默认情况下内部 boot rom 会将 I.MX6U 的主

【Power Compiler手册】9.时钟门控(4修改时钟门控结构)

修改时钟门控结构 在执行 RTL 时钟门控时,可以指定 `set_clock_gating_style -max_fanout` 命令来限制由单个时钟门控元素门控的寄存器数量。结果可能是具有相同使能信号的多个时钟门控元素,并且在逻辑上,具有相同的门控时钟信号。所有具有相同使能信号的时钟门控单元属于同一个时钟门控组。由单个时钟门控元素门控的所有寄存器属于同一个时钟门控子组。 由 `compi

vue实现带日期星期数字时钟代码

<template ><view ><view>{{date}}</view><view>{{time}}</view></view></template><script>import service from '../../service.js';export default {data:{date:'',timerID:'',time:'',week:['星期日', '星期一', '星期二',