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.pswp.cn/bicheng/79340.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/79340.shtml
英文地址,請注明出處:http://en.pswp.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;從數據庫讀取權限信息 我們從數據庫中提取與用戶、…