Qt指南针

        Qt写的指南针demo.

      运行结果

        滑动调整指针角度

 实现代码

        h文件

#ifndef COMPASS_H
#define COMPASS_H#include <QWidget>
#include <QColor>class Compass : public QWidget
{Q_OBJECT// 可自定义属性Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor)Q_PROPERTY(QColor needleColor READ needleColor WRITE setNeedleColor)Q_PROPERTY(QColor directionColor READ directionColor WRITE setDirectionColor)Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)Q_PROPERTY(double angle READ angle WRITE setAngle)Q_PROPERTY(int borderWidth READ borderWidth WRITE setBorderWidth)public:explicit Compass(QWidget *parent = nullptr);// 获取和设置属性QColor backgroundColor() const;void setBackgroundColor(const QColor &color);QColor borderColor() const;void setBorderColor(const QColor &color);QColor needleColor() const;void setNeedleColor(const QColor &color);QColor directionColor() const;void setDirectionColor(const QColor &color);QColor textColor() const;void setTextColor(const QColor &color);double angle() const;void setAngle(double angle);int borderWidth() const;void setBorderWidth(int width);QSize sizeHint() const override;QSize minimumSizeHint() const override;protected:void paintEvent(QPaintEvent *event) override;private:void drawCompassCircle(QPainter &painter);void drawDirectionMarkers(QPainter &painter);void drawNeedle(QPainter &painter);void drawTechRing(QPainter &painter);QColor m_backgroundColor;QColor m_borderColor;QColor m_needleColor;QColor m_directionColor;QColor m_textColor;double m_angle;int m_borderWidth;
};#endif // COMPASS_H

        c文件

#include "widget.h"
#include <QPainter>
#include <QPainterPath>
#include <QtMath>
#include <QLinearGradient>Compass::Compass(QWidget *parent) : QWidget(parent),m_backgroundColor(QColor(30, 30, 40)),m_borderColor(QColor(80, 160, 255)),m_needleColor(QColor(255, 80, 80)),m_directionColor(QColor(80, 255, 160)),m_textColor(QColor(220, 220, 220)),m_angle(0),m_borderWidth(3)
{setMinimumSize(100, 100);
}QColor Compass::backgroundColor() const { return m_backgroundColor; }
void Compass::setBackgroundColor(const QColor &color) { m_backgroundColor = color; update(); }QColor Compass::borderColor() const { return m_borderColor; }
void Compass::setBorderColor(const QColor &color) { m_borderColor = color; update(); }QColor Compass::needleColor() const { return m_needleColor; }
void Compass::setNeedleColor(const QColor &color) { m_needleColor = color; update(); }QColor Compass::directionColor() const { return m_directionColor; }
void Compass::setDirectionColor(const QColor &color) { m_directionColor = color; update(); }QColor Compass::textColor() const { return m_textColor; }
void Compass::setTextColor(const QColor &color) { m_textColor = color; update(); }double Compass::angle() const { return m_angle; }
void Compass::setAngle(double angle) { m_angle = angle; update(); }int Compass::borderWidth() const { return m_borderWidth; }
void Compass::setBorderWidth(int width) { m_borderWidth = width; update(); }QSize Compass::sizeHint() const { return QSize(200, 200); }
QSize Compass::minimumSizeHint() const { return QSize(100, 100); }void Compass::paintEvent(QPaintEvent *event)
{Q_UNUSED(event);QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing, true);// 绘制指南针背景drawCompassCircle(painter);// 绘制方向标记drawDirectionMarkers(painter);// 绘制科技感圆环drawTechRing(painter);// 绘制指针drawNeedle(painter);
}void Compass::drawCompassCircle(QPainter &painter)
{int side = qMin(width(), height());QRectF outerRect(0, 0, side, side);outerRect.moveCenter(rect().center());// 绘制背景painter.setPen(Qt::NoPen);painter.setBrush(m_backgroundColor);painter.drawEllipse(outerRect);// 绘制边框QPen borderPen(m_borderColor);borderPen.setWidth(m_borderWidth);painter.setPen(borderPen);painter.setBrush(Qt::NoBrush);painter.drawEllipse(outerRect);// 绘制内圆QRectF innerRect = outerRect.adjusted(side*0.1, side*0.1, -side*0.1, -side*0.1);QRadialGradient gradient(innerRect.center(), innerRect.width()/2);gradient.setColorAt(0, QColor(50, 50, 60));gradient.setColorAt(1, QColor(20, 20, 30));painter.setPen(Qt::NoPen);painter.setBrush(gradient);painter.drawEllipse(innerRect);
}void Compass::drawDirectionMarkers(QPainter &painter)
{int side = qMin(width(), height());QRectF outerRect(0, 0, side, side);outerRect.moveCenter(rect().center());painter.save();painter.translate(outerRect.center());QFont font = painter.font();font.setPixelSize(side * 0.08);font.setBold(true);painter.setFont(font);painter.setPen(m_textColor);QStringList directions = {"N", "E", "S", "W"};QStringList subDirections = {"NE", "SE", "SW", "NW"};// 绘制主要方向标记for (int i = 0; i < 4; ++i) {painter.save();painter.rotate(i * 90);// 绘制刻度线QPen pen(m_directionColor);pen.setWidth(side * 0.01);painter.setPen(pen);painter.drawLine(0, -outerRect.height()*0.45, 0, -outerRect.height()*0.35);// 绘制方向文字painter.translate(0, -outerRect.height()*0.3);painter.rotate(-i * 90);painter.drawText(QRect(-side*0.1, -side*0.1, side*0.2, side*0.2),Qt::AlignCenter, directions[i]);painter.restore();}// 绘制次要方向标记for (int i = 0; i < 4; ++i) {painter.save();painter.rotate(45 + i * 90);// 绘制刻度线QPen pen(m_directionColor);pen.setWidth(side * 0.005);painter.setPen(pen);painter.drawLine(0, -outerRect.height()*0.45, 0, -outerRect.height()*0.38);// 绘制方向文字painter.translate(0, -outerRect.height()*0.33);painter.rotate(-45 - i * 90);painter.drawText(QRect(-side*0.1, -side*0.1, side*0.2, side*0.2),Qt::AlignCenter, subDirections[i]);painter.restore();}// 绘制更小的刻度for (int i = 0; i < 36; ++i) {if (i % 9 == 0) continue; // 跳过主要方向painter.save();painter.rotate(i * 10);QPen pen(m_textColor);pen.setWidth(side * 0.003);painter.setPen(pen);if (i % 3 == 0) {// 中等刻度painter.drawLine(0, -outerRect.height()*0.45, 0, -outerRect.height()*0.4);} else {// 小刻度painter.drawLine(0, -outerRect.height()*0.45, 0, -outerRect.height()*0.42);}painter.restore();}painter.restore();
}void Compass::drawNeedle(QPainter &painter)
{int side = qMin(width(), height());QRectF outerRect(0, 0, side, side);outerRect.moveCenter(rect().center());painter.save();painter.translate(outerRect.center());painter.rotate(m_angle);// 创建科技感指针形状QPainterPath needlePath;// 主指针needlePath.moveTo(0, -outerRect.height()*0.35);needlePath.lineTo(-outerRect.width()*0.05, -outerRect.height()*0.05);needlePath.lineTo(0, 0);needlePath.lineTo(outerRect.width()*0.05, -outerRect.height()*0.05);needlePath.closeSubpath();// 尾翼needlePath.moveTo(0, outerRect.height()*0.1);needlePath.lineTo(-outerRect.width()*0.03, outerRect.height()*0.2);needlePath.lineTo(0, outerRect.height()*0.25);needlePath.lineTo(outerRect.width()*0.03, outerRect.height()*0.2);needlePath.closeSubpath();// 绘制指针QLinearGradient gradient(0, -outerRect.height()*0.35, 0, outerRect.height()*0.25);gradient.setColorAt(0, m_needleColor.lighter(150));gradient.setColorAt(1, m_needleColor.darker(150));painter.setPen(QPen(m_needleColor.darker(200), side*0.005));painter.setBrush(gradient);painter.drawPath(needlePath);// 绘制中心圆点QRadialGradient centerGradient(0, 0, side*0.05);centerGradient.setColorAt(0, Qt::white);centerGradient.setColorAt(1, m_needleColor);painter.setPen(Qt::NoPen);painter.setBrush(centerGradient);painter.drawEllipse(QRectF(-side*0.05, -side*0.05, side*0.1, side*0.1));painter.restore();
}void Compass::drawTechRing(QPainter &painter)
{int side = qMin(width(), height());QRectF outerRect(0, 0, side, side);outerRect.moveCenter(rect().center());painter.save();painter.translate(outerRect.center());// 绘制外环科技感装饰QPen techPen(m_borderColor);techPen.setWidth(side * 0.01);painter.setPen(techPen);// 绘制分段圆环for (int i = 0; i < 8; ++i) {painter.save();painter.rotate(i * 45);QRectF segmentRect(-side*0.5, -side*0.5, side, side);segmentRect.adjust(side*0.05, side*0.05, -side*0.05, -side*0.05);int startAngle = 10 * 16;int spanAngle = 25 * 16;painter.drawArc(segmentRect, startAngle, spanAngle);painter.restore();}// 绘制内环科技感装饰QRectF innerRingRect(-side*0.3, -side*0.3, side*0.6, side*0.6);techPen.setColor(m_directionColor);techPen.setWidth(side * 0.005);painter.setPen(techPen);painter.setBrush(Qt::NoBrush);painter.drawEllipse(innerRingRect);// 绘制内环点阵for (int i = 0; i < 36; ++i) {painter.save();painter.rotate(i * 10);QPointF point(0, -side*0.25);painter.setPen(Qt::NoPen);painter.setBrush(m_directionColor);painter.drawEllipse(point, side*0.005, side*0.005);painter.restore();}painter.restore();
}

        main.c

#include "widget.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QSlider>
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);QWidget window;QVBoxLayout *layout = new QVBoxLayout(&window);// 创建指南针控件Compass *compass = new Compass();compass->setBackgroundColor(QColor(20, 25, 35));compass->setBorderColor(QColor(0, 200, 255));compass->setNeedleColor(QColor(255, 60, 60));compass->setDirectionColor(QColor(0, 255, 180));compass->setTextColor(Qt::white);compass->setBorderWidth(4);// 创建滑块控制方向QSlider *slider = new QSlider(Qt::Horizontal);slider->setRange(0, 360);slider->setValue(0);QObject::connect(slider, &QSlider::valueChanged, [compass](int value) {compass->setAngle(value);});layout->addWidget(compass, 1);layout->addWidget(slider);window.resize(400, 450);window.show();return a.exec();
}

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

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

相关文章

北大新媒体运营黄金提示词 | 北大Deepseek系列第七弹《DeepSeek与新媒体运营》,13所大学系列一站下载

今天大师兄给大家推荐的是北京大学Deepseek系列第七弹《DeepSeek与新媒体运营》。 本文档系统介绍了DeepSeek模型在新媒体运营中的应用&#xff0c;技术原理、实践案例及行业挑战。 适用人群&#xff1a;新媒体运营人员、AI研究者、企业决策者。 思维导图 napkin生成 《老…

2025年真实面试问题汇总(一)

Spingboot中如何实现有些类是否加载 在 Spring Boot 中可以通过 条件化配置&#xff08;Conditional Configuration&#xff09; 来控制某些类是否加载。Spring Boot 提供了一系列 Conditional 注解&#xff0c;允许根据特定条件动态决定 Bean 或配置类是否生效。以下是常见的…

综合案例建模(2)

文章目录 螺旋片端盖多孔扭转环作业一作业二作业三 螺旋片端盖 上视基准面画草图&#xff0c;拉伸250&#xff0c;向外拔模15度 以地面圆&#xff08;如果不行就转换实体引用&#xff09;&#xff0c;创建螺旋线&#xff0c;锥形螺纹线15度向外 前视基准面去画草图 以上一步草图…

Qt5与现代OpenGL学习(三)纹理

把第一张图放到D盘的1文件夹里面&#xff1a;1.png triangle.h #ifndef WIDGET_H #define WIDGET_H#include <QOpenGLWidget> #include <QOpenGLFunctions> #include <QOpenGLVertexArrayObject> #include <QOpenGLShaderProgram> #include <QOpen…

这是一款好用的PDF工具!

用户习惯有时确实非常顽固&#xff0c;想要改变它可能需要漫长的时间。 比如PDF软件&#xff0c;我认为国产的福/昕、万/兴等软件都非常不错&#xff0c;它们贴合国人的使用习惯&#xff0c;操作起来非常顺手。但因为我习惯使用DC&#xff0c;所以在处理PDF文档时&#xff0c;…

轻松实现CI/CD: 用Go编写的命令行工具简化Jenkins构建

在工作中&#xff0c;随着开发维护的服务越来越多&#xff0c;在很长的一段时间里&#xff0c;我来回在多个服务之间开发、构建、查看容器是否启动成功。尤其是开发测试阶段&#xff0c;需要打开jenkins页面、搜索应用、再构建、再打开rancher页面、搜索应用&#xff0c;这一连…

第十六届蓝桥杯 2025 C/C++B组第一轮省赛 全部题解(未完结)

目录 前言&#xff1a; 试题A&#xff1a;移动距离 试题C&#xff1a;可分解的正整数 试题D&#xff1a;产值调整 试题E&#xff1a;画展布置 前言&#xff1a; 我参加的是第一轮省赛&#xff0c;说实话第一次参加还是比较紧张的&#xff0c;真到考场上看啥都想打暴力&…

Qt Creator环境编译的Release软件放在其他电脑上使用方法

本文解决的问题&#xff1a;将Qt Creator环境编译的exe可执行程序放到其他电脑上不可用情况 1、寻找windeployqt工具所在路径" D:\Qt5.12.10\5.12.10\msvc2015_64\bin" &#xff0c;将此路径配置到环境变量&#xff1b; 2、用Qt Creator环境编译出Release版本可执行…

使用skywalking进行go的接口监控和报警

安装 helm upgrade --install skywalking ./skywalking-v1 --namespace skywalking --create-namespace 查看安装结果 kubectl get pod -n skywalking NAME READY STATUS RESTARTS AGE elasticsearch-6c4ccbf99f-ng6sk 1/1 …

2025年- H16-Lc124-169.多数元素(技巧)---java版

1.题目描述 2.思路 3.代码实现 import java.util.Arrays;public class H169 {public int majorityElement(int[] nums) {Arrays.sort(nums);int nnums.length;return nums[n/2];}public static void main(String[] args){H169 test07new H169();int[] nums{2,2,1,1,1,2,2};int…

k8s术语pod

Pod概览 理解Pod Pod是kubernetes中你可以创建和部署的最小也是最简的单位,pod代表着集群中运行的进程。 Pod中封装着应用的容器(有的情况下是好几个容器),存储、独立的网络IP,管理容器如何运行的策略选项。Pod代表着部署的一个单位:kubemetes中应用的一个实例,可能由一个…

《数字图像处理(面向新工科的电工电子信息基础课程系列教材)》章节思维导图

今天看到了几本书的思维导图&#xff0c;感触颇深&#xff0c;如果思维导图只是章节安排&#xff0c;这样的思维导图有毛用。 给出《数字图像处理&#xff08;面向新工科的电工电子信息基础课程系列教材&#xff09;》实质内容章节的思维导图。思维导图的优势是逻辑关系和知识…

Nacos简介—4.Nacos架构和原理二

大纲 1.Nacos的定位和优势 2.Nacos的整体架构 3.Nacos的配置模型 4.Nacos内核设计之一致性协议 5.Nacos内核设计之自研Distro协议 6.Nacos内核设计之通信通道 7.Nacos内核设计之寻址机制 8.服务注册发现模块的注册中心的设计原理 9.服务注册发现模块的注册中心的服务数…

【MySQL】复合查询与内外连接

目录 一、复合查询 1、基本查询回顾&#xff1a; 2、多表查询&#xff1a; 3、自连接&#xff1a; 4、子查询&#xff1a; 单列子查询 多行子查询&#xff1a; 多列子查询&#xff1a; 在from语句中使用子查询&#xff1a; 5、合并查询&#xff1a; union&#xff1…

后端工程师需要掌握哪些基础技能

后端工程师是构建系统核心逻辑的关键角色&#xff0c;需要掌握从基础到进阶的完整技术栈。以下是结合国内实际开发需求的技能树整理&#xff0c;附带学习建议&#xff1a; 一、编程语言&#xff08;至少精通1-2种&#xff09; # 国内主流选择&#xff08;按优先级排序&#x…

万字重谈C++——继承篇

继承的概念及定义 继承的概念 继承&#xff08;Inheritance&#xff09;机制作为面向对象程序设计中最核心的代码复用方式&#xff0c;它不仅允许开发人员在保留基础类特性的前提下进行功能扩展&#xff08;从而创建新的派生类&#xff09;&#xff0c;更重要的是体现了面向对…

移动光猫 UNG853H 获取超级管理员账号密码

注&#xff1a;电脑连接光猫&#xff0c;网线不要接2口&#xff08;2口一般是IPTV网口&#xff09; 首先浏览器打开 192.168.1.1&#xff0c;使用光猫背面的用户名密码登录。&#xff08;user用户名&#xff09; 然后在浏览器中另开一个窗口打开以下地址&#xff1a; http://…

ActiveMQ 可靠性保障:消息确认与重发机制(二)

ActiveMQ 重发机制 重发机制的原理与触发条件 ActiveMQ 的重发机制是确保消息可靠传输的重要手段。当消息发送到 ActiveMQ 服务器后&#xff0c;如果消费者由于某些原因未能成功处理消息&#xff0c;ActiveMQ 会依据配置的重发策略&#xff0c;将消息重新放入队列或主题中&am…

oceanbase设置密码

docker run -p 2881:2881 --name oceanbase-ce -e MODEmini -d oceanbase/oceanbase-ce:4.2.1.10-110010012025041414 先进入镜像再连接数据库的方式 进入镜像 docker exec -it oceanbase-ce bash 修改数据库密码 ALTER USER ‘root’ IDENTIFIED BY ‘123456’; 无密码 obc…

使用Python和Pandas实现的Azure Synapse Dedicated SQL pool权限检查与SQL生成用于IT审计

下面是使用 Python Pandas 来提取和展示 Azure Synapse Dedicated SQL Pool 中权限信息的完整过程&#xff0c;同时将其功能以自然语言描述&#xff0c;并自动构造所有权限设置的 SQL 语句&#xff1a; ✅ 步骤 1&#xff1a;从数据库读取权限信息 我们从数据库中提取与用户、…