Qt+C++自定義控件儀表盤動畫仿真

程序示例精選

Qt+C++自定義控件儀表盤動畫仿真

如需安裝運行環境或遠程調試,見文章底部個人QQ名片,由專業技術人員遠程協助!

前言

這篇博客針對<<Qt+C++自定義控件儀表盤動畫仿真>>編寫代碼,代碼整潔,規則,易讀。 學習與應用推薦首選。


文章目錄

一、所需工具軟件

二、使用步驟

????????1. 引入庫

????????2. 代碼實現

? ? ? ? 3. 運行結果

三、在線協助

一、所需工具軟件

1. VS, Qt

2. C++

二、使用步驟

1.引入庫

#include <QWidget>
#include <QPropertyAnimation>
#include <QtMath>
#include <QPainter>

2. 代碼實現

代碼如下:

#include "GaugePanel.h"
GaugePanel::~GaugePanel()
{hShearAnimation->stop();vShearAnimation->stop();delete hShearAnimation;delete vShearAnimation;
}void GaugePanel::paintEvent(QPaintEvent*)
{int width = this->width();int height = this->height();int side = qMin(width, height);//繪制準備工作,啟用反鋸齒,平移坐標軸中心,等比例縮放QPainter painter(this);painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);painter.translate(width / 2, height / 2);painter.scale(side / 215.0, side / 215.0);painter.shear(double(hShearValue / 100.0f), double(vShearValue / 100.0f));//內層漸變drawInnerGradient(&painter);//外層漸變drawOuterGradient(&painter);//外層光暈drawOuterHalo(&painter);//刻度線drawScale(&painter);//刻度值drawScaleNum(&painter);//繪制指針drawPointer(&painter);//繪制指針扇形drawPointerSector(&painter);//繪制值drawValue(&painter);//繪制單位drawUnit(&painter);
}void GaugePanel::drawOuterGradient(QPainter* painter)
{if (radiusHalo <= radiusOuter)return;painter->save();QRectF rectangle(0 - radiusHalo, 0 - radiusHalo, radiusHalo * 2, radiusHalo * 2);QPen framePen(colorOuterFrame);framePen.setWidthF(1.5f);painter->setPen(framePen);painter->drawEllipse(rectangle);painter->setPen(Qt::NoPen);QPainterPath smallCircle;QPainterPath bigCircle;float radius = radiusOuter;smallCircle.addEllipse(-radius, -radius, radius * 2, radius * 2);radius += (radiusHalo - radiusOuter);bigCircle.addEllipse(-radius, -radius, radius * 2, radius * 2);//大圓拋去小圓部分QPainterPath gradientPath = bigCircle - smallCircle;QRadialGradient gradient(0, 0, radius, 0, 0);//gradient.setSpread(QGradient::ReflectSpread);gradient.setColorAt(0.85, colorOuterStart);gradient.setColorAt(0.98, colorOuterEnd);painter->setBrush(gradient);painter->drawPath(gradientPath);painter->restore();
}void GaugePanel::drawInnerGradient(QPainter* painter)
{if (radiusOuter <= radiusInner)return;painter->save();painter->setPen(Qt::NoPen);QPainterPath smallCircle;QPainterPath bigCircle;float radius = radiusInner;smallCircle.addEllipse(-radius, -radius, radius * 2, radius * 2);radius += (radiusOuter - radiusInner);bigCircle.addEllipse(-radius, -radius, radius * 2, radius * 2);//大圓拋去小圓部分QPainterPath gradientPath = bigCircle - smallCircle;QRadialGradient gradient(0, 0, radius, 0, 0);//gradient.setSpread(QGradient::ReflectSpread);gradient.setColorAt(0.7, colorInnerStart);gradient.setColorAt(1, colorInnerEnd);painter->setBrush(gradient);painter->drawPath(gradientPath);painter->restore();
}void GaugePanel::drawOuterHalo(QPainter* painter)
{painter->save();painter->setPen(Qt::NoPen);QPainterPath smallCircle;QPainterPath bigCircle;float radius = radiusHalo;smallCircle.addEllipse(-radius, -radius, radius * 2, radius * 2);radius += (110.0 - radiusHalo);bigCircle.addEllipse(-radius, -radius, radius * 2, radius * 2);//大圓拋去小圓部分QPainterPath gradientPath = bigCircle - smallCircle;QRadialGradient gradient(0, 0, 100, 0, 0);gradient.setSpread(QGradient::ReflectSpread);gradient.setColorAt(radiusHalo / 100, colorHaloStart);gradient.setColorAt(1, colorHaloEnd);painter->setBrush(gradient);painter->drawPath(gradientPath);painter->restore();
}void GaugePanel::drawScale(QPainter* painter)
{float radius = 85;painter->save();painter->setPen(QColor(255, 255, 255));painter->rotate(30);int steps = (30);double angleStep = (360.0 - 60) / steps;QPen pen = painter->pen();pen.setCapStyle(Qt::RoundCap);for (int i = 0; i <= steps; i++) {if (i % 3 == 0) {pen.setWidthF(1.5);painter->setPen(pen);QLineF line(0.0f, radius - 8.0f, 0.0f, radius);painter->drawLine(line);}else {pen.setWidthF(0.5);painter->setPen(pen);QLineF line(0.0f, radius - 3.0f, 0.0f, radius);painter->drawLine(line);}painter->rotate(angleStep);}painter->restore();
}void GaugePanel::drawScaleNum(QPainter* painter)
{float radius = 95.0f;painter->save();painter->setPen(QColor(255, 255, 255));double startRad = (330 - 90) * (M_PI / 180);double deltaRad = (300) * (M_PI / 180) / 10;for (int i = 0; i <= 10; i++) {double sina = sin(startRad - i * deltaRad);double cosa = cos(startRad - i * deltaRad);double value = 1.0 * i * ((30) / 10);//刻度值范圍QString strValue = QString("%1").arg((double)value, 0, 'f', 0);double textWidth = fontMetrics().width(strValue);double textHeight = fontMetrics().height();int x = radius * cosa - textWidth / 2;int y = -radius * sina + textHeight / 4;painter->drawText(x, y, strValue);}painter->restore();
}void GaugePanel::drawPointer(QPainter* painter)
{painter->save();float radius = 83.0;painter->rotate(30 + int(value * 10));QPen pen = painter->pen();pen.setWidthF(1.0);pen.setColor(QColor(50, 154, 255, 200));painter->setPen(pen);QLineF line(0.0f, 0.0f, 0.0f, radius);painter->drawLine(line);painter->restore();
}void GaugePanel::drawPointerSector(QPainter* painter)
{float radius = 87.5f;painter->save();painter->setPen(Qt::NoPen);QRectF rect(-radius, -radius, radius * 2, radius * 2);painter->setBrush(QColor(50, 154, 255, 50));painter->drawPie(rect, -120 * 16, -value * 16 * 10);painter->restore();
}void GaugePanel::drawValue(QPainter* painter)
{int radius = 100;painter->save();painter->setPen(QColor(255, 255, 255));painter->setFont(QFont("Arial", 22, 22, true));QRectF textRect(-radius, -radius, radius * 2, radius * 2);QString strValue = QString("%1").arg((double)value, 0, 'f', 0);painter->drawText(textRect, Qt::AlignCenter, strValue);painter->restore();
}void GaugePanel::drawUnit(QPainter* painter)
{int radius = 100;painter->save();painter->setPen(QColor(255, 255, 255));painter->setFont(QFont("Arial", 9, -1, true));QRectF textRect(-radius, -radius + 20, radius * 2, radius * 2);painter->drawText(textRect, Qt::AlignCenter, "km/h");painter->restore();
}double GaugePanel::getValue() const
{return this->value;
}int GaugePanel::getHShearValue() const
{return this->hShearValue;
}int GaugePanel::getVShearValue() const
{return this->vShearValue;
}double GaugePanel::getRadiusInner() const
{return radiusInner;
}double GaugePanel::getRadiusOuter() const
{return radiusOuter;
}double GaugePanel::getRadiusHalo() const
{return radiusHalo;
}QColor GaugePanel::getColorOuterFrame() const
{return colorOuterFrame;
}QColor GaugePanel::getColorInnerStart() const
{return colorInnerStart;
}QColor GaugePanel::getColorInnerEnd() const
{return colorInnerEnd;
}QColor GaugePanel::getColorOuterStart() const
{return colorOuterStart;
}QColor GaugePanel::getColorOuterEnd() const
{return colorOuterEnd;
}QColor GaugePanel::getColorHaloStart() const
{return colorHaloStart;
}QColor GaugePanel::getColorHaloEnd() const
{return colorHaloEnd;
}void GaugePanel::setValue(int value)
{setValue(double(value));
}void GaugePanel::setValue(double value) {updateValue(value);
}void GaugePanel::setHShearValue(int value)
{if (value > 100 || value < -100)return;this->hShearValue = value;update();
}void GaugePanel::setVShearValue(int value)
{if (value > 100 || value < -100)return;this->vShearValue = value;update();
}void GaugePanel::setColorOuterFrame(QColor color)
{colorOuterFrame = color;
}void GaugePanel::setRadiusInner(int radius)
{setRadiusInner(double(radius));
}void GaugePanel::setRadiusInner(double radius)
{if (radius >= 0.0f && radius < 100.0f) {radiusInner = radius;update();}
}void GaugePanel::setRadiusOuter(int radius)
{setRadiusOuter(double(radius));
}void GaugePanel::setRadiusOuter(double radius)
{if (radius > 0.0f && radius < 100.0f) {radiusOuter = radius;update();}
}void GaugePanel::setRadiusHalo(int radius)
{setRadiusHalo(double(radius));
}void GaugePanel::setRadiusHalo(double radius)
{if (radius > 0.0f && radius < 100.0f) {radiusHalo = radius;update();}
}void GaugePanel::setColorInnerStart(QColor color)
{colorInnerStart = color;
}void GaugePanel::setColorInnerEnd(QColor color)
{colorInnerEnd = color;
}void GaugePanel::setColorOuterStart(QColor color)
{colorOuterStart = color;
}void GaugePanel::setColorOuterEnd(QColor color)
{colorOuterEnd = color;
}void GaugePanel::setColorHaloStart(QColor color)
{colorHaloStart = color;
}void GaugePanel::setColorHaloEnd(QColor color)
{colorHaloEnd = color;
}void GaugePanel::startShearAnimal(int duration, int hShearValue, int vShearValue)
{if (hShearValue == this->hShearValue && vShearValue == this->vShearValue) {return;}if (hShearAnimation->state() != QPropertyAnimation::Stopped) {hShearAnimation->stop();}if (vShearAnimation->state() != QPropertyAnimation::Stopped) {vShearAnimation->stop();}hShearAnimation->setDuration(duration);hShearAnimation->setStartValue(this->hShearValue);hShearAnimation->setEndValue(hShearValue);hShearAnimation->start();vShearAnimation->setDuration(duration);vShearAnimation->setStartValue(this->vShearValue);vShearAnimation->setEndValue(vShearValue);vShearAnimation->start();
}void GaugePanel::updateValue(double value)
{if (value > 30.0 || value < 0.0) {return;}this->value = value;//update();this->update();// emit valueChanged(value);
}

3. 運行結果

?

三、在線協助:

如需安裝運行環境或遠程調試,見文章底部個人 QQ 名片,由專業技術人員遠程協助!
1)遠程安裝運行環境,代碼調試
2)Qt, C++, Python入門指導
3)界面美化
4)軟件制作

當前文章連接:Python+Qt桌面端與網頁端人工客服溝通工具_alicema1111的博客-CSDN博客

博主推薦文章:python人臉識別統計人數qt窗體-CSDN博客

博主推薦文章:Python Yolov5火焰煙霧識別源碼分享-CSDN博客

? ? ? ? ? ? ? ? ? ? ? ? ?Python OpenCV識別行人入口進出人數統計_python識別人數-CSDN博客

個人博客主頁:alicema1111的博客_CSDN博客-Python,C++,網頁領域博主

博主所有文章點這里alicema1111的博客_CSDN博客-Python,C++,網頁領域博主

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/36473.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/36473.shtml
英文地址,請注明出處:http://en.pswp.cn/news/36473.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

2023國賽數學建模D題思路分析

文章目錄 0 賽題思路1 競賽信息2 競賽時間3 建模常見問題類型3.1 分類問題3.2 優化問題3.3 預測問題3.4 評價問題 4 建模資料 0 賽題思路 &#xff08;賽題出來以后第一時間在CSDN分享&#xff09; https://blog.csdn.net/dc_sinor?typeblog 1 競賽信息 全國大學生數學建模…

P8605 [藍橋杯 2013 國 AC] 網絡尋路

X 國的一個網絡使用若干條線路連接若干個節點。節點間的通信是雙向的。某重要數據包&#xff0c;為了安全起見&#xff0c;必須恰好被轉發兩次到達目的地。該包可能在任意一個節點產生&#xff0c;我們需要知道該網絡中一共有多少種不同的轉發路徑。 源地址和目標地址可以相同…

PHP原生類

什么是php原生類 原生類就是php內置類&#xff0c;不用定義php自帶的類&#xff0c;即不需要在當前腳本寫出&#xff0c;但也可以實例化的類 我們可以通過腳本找一下php原生類 <?php $classes get_declared_classes(); foreach ($classes as $class) {$methods get_clas…

網頁touch-action禁用手勢

問題 在iphone的safari 下滑網頁時&#xff0c;無法下滑 原因&#xff1a; 設置了touch-action: none html ,body {touch-action: none;}此行代碼為禁用手勢&#xff0c;但是在pc上Chrome瀏覽器模擬手機時&#xff0c;并不能生效(禁止手勢)。 但是此行代碼在iphone手機瀏覽器…

【Go 基礎篇】Go語言字符類型:解析字符的本質與應用

介紹 字符類型是計算機編程中用于表示文本和字符的數據類型&#xff0c;是構建字符串的基本單位。在Go語言&#xff08;Golang&#xff09;中&#xff0c;字符類型具有獨特的特點和表示方式&#xff0c;包括Unicode編碼、字符字面值以及字符操作。本篇博客將深入探討Go語言中的…

首次使用ninja的體驗

首先總結說自己的理解&#xff0c;就是NINJA是一個和MAKE同一級別的編譯工具&#xff0c;在CMAKE/GRADLE等工具之下工作 cmake目前可以生成makefile&#xff0c;也可以生成ninja文件(CMAKE選項中增加了-G Ninja&#xff09; 使用ninja all編譯生成的ninja文件 1.工具準備&…

Element組件淺嘗輒止5:Empty 空狀態組件

Empty空狀態組件&#xff1a;空狀態時的占位提示。 如第一次進入當前功能模塊時&#xff0c;數據狀態為空&#xff0c;則展示空狀態&#xff0c;可用到Empty組件 1.How? <el-empty description"描述文字"></el-empty> 2.自定義圖片 通過設置 image 屬…

plsql開發中動態sql的使用教程(不使用dbms_sql包)

一般的PL/SQL程序設計中&#xff0c;在DML和事務控制的語句中可以直接使用SQL&#xff0c;但是對于新建存儲過程&#xff0c;其中涉及傳參要被應用為列名時&#xff0c;不能在PL/SQL中直接使用&#xff0c;一會兒下面舉例介紹&#xff0c;那么要想實現設計的功能&#xff0c;可…

PyTorch Lightning教程七:可視化

本節指導如何利用Lightning進行可視化和監控模型 為何需要跟蹤參數 在模型開發中&#xff0c;我們跟蹤感興趣的值&#xff0c;例如validation_loss&#xff0c;以可視化模型的學習過程。模型開發就像駕駛一輛沒有窗戶的汽車&#xff0c;圖表和日志提供了窗口&#xff0c;讓我們…

Docker的基本概念及鏡像加速器的配置

1.Docker的概念 由于代碼運行環境不同&#xff0c;代碼運行會出現水土不服的情況。運用docker容器會把環境進行打包&#xff0c;避免水土不服。docker是一種容器技術&#xff0c;它解決軟件跨環境遷移的問題。 2&#xff0c;安裝Docker 3.Docker架構 4.Docker鏡像加速器的配…

我們常說這個pycharm里有陷阱,第三方庫導入失敗,看這里!

最近有小伙伴遇到了明明安裝了 python 第三方庫&#xff0c;但是在 pycharm 當中卻導入不成功的問題。 ? 一直以來&#xff0c;也有不少初學 python 的小伙伴&#xff0c;一不小心就跳進了虛擬環境和系統環境的【陷阱】中。 本文就基于此問題&#xff0c;來說說在 pycharm 當…

Spring Cloud 面試突擊2

Spring Cloud 面試突擊2 高并發&#xff1a;是一種系統運行過程中遇到的短時間大量的請求操作 響應時間&#xff1a; 吞吐量&#xff1a; QPS&#xff1a;數據庫為維度 TPS 并發用戶數 并發的維度&#xff1a;很多的 并發是不是達到的當前系統的瓶頸 緩存 &#xff08…

計算機網絡—IP

這里寫目錄標題 IP的基本認識網絡層與數據鏈路層有什么關系IP地址基礎知識IP 地址的分類什么是A、B、C類地址廣播地址用來做什么什么是D、E類廣播多播地址用于什么IP分類的優點IP分類的缺點 無分類地址CIDR如何劃分網絡號和主機號怎么進性子網劃分 公有 IP 地址與私有 IP 地址公…

【Spring源碼】Java里面的jdk代理與Cglib動態代理

Springboot默認使用的是Cglib動態代理 案例一&#xff1a;TransactionAutoConfiguration配置類 org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration Configuration(proxyBeanMethods false)ConditionalOnBean(TransactionManager.class)Cond…

指針---進階篇(二)

指針---進階篇&#xff08;二&#xff09; 前言一、函數指針1.拋磚引玉2.如何判斷函數指針&#xff1f;&#xff08;方法總結&#xff09; 二、函數指針數組1.什么是函數指針數組&#xff1f;2.講解函數指針數組3.模擬計算器&#xff1a;講解函數指針數組 三、指向函數指針數組…

Maven基礎之倉庫、命令、插件機制

文章目錄 Maven 倉庫中央倉庫和本地倉庫中央倉庫本地倉庫 Maven 命令generate 命令compile 命令clean 命令test 命令package 命令install 命令 Maven 插件機制官方插件&#xff1a;Compile 插件Tomcat 7 插件 Maven 倉庫 中央倉庫和本地倉庫 [?] 簡單一點說 中央倉庫是一個網…

Redis復制

在Redis中&#xff0c;用戶可以通過執行SLAVEOF命令或者設置slaveof選項&#xff0c;讓一個服務器去復制(replicate) 另一個服務器&#xff0c;我們稱呼被復制的服務器為主服務器(master)&#xff0c;而對主服務器進行復制的服務器則被稱為從服務器(slave)&#xff0c;如下圖所…

Vue修飾符

事件修飾符 在Vue 2.0中&#xff0c;事件修飾符允許我們在處理事件時對其進行修改或增強。以下是一些常用的事件修飾符&#xff1a; .stop&#xff1a;阻止事件冒泡。使用此修飾符后&#xff0c;父元素的相同事件不會再觸發。.prevent&#xff1a;阻止事件的默認行為。比如&…

mybatis 中的<![CDATA[ ]]>用法及說明

<![CDATA[ ]]>作用 <![CDATA[ ]]> 在mybatis、ibatis等書寫SQL的xml中比較常見&#xff0c;是一種XML語法&#xff0c;他的作用是 可以忽略xml的轉義&#xff08;在該標簽中的語句和字符原本是什么樣的&#xff0c;在拼接成SQL后還是什么樣的&#xff09; 使用&a…

代碼生成模型任務設計

背景&#xff1a; 模型應該具備&#xff0c;理解代碼的能力、知道代碼規則的能力、知道關鍵詞和變量的能力、知道代碼邏輯的能力、文本到代碼翻譯能力、代碼關聯能力、代碼續寫能力。 代碼理解能力&#xff1a;pretrain讓模型讀足夠多代碼、記住代碼一些規則、代碼問答、基于…