国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

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

這篇具有很好參考價值的文章主要介紹了Qt+C++自定義控件儀表盤動畫仿真。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

程序示例精選

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

如需安裝運行環(huán)境或遠程調(diào)試,見文章底部個人QQ名片,由專業(yè)技術(shù)人員遠程協(xié)助!

前言

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


文章目錄

一、所需工具軟件

二、使用步驟

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

????????2. 代碼實現(xiàn)

? ? ? ? 3. 運行結(jié)果

三、在線協(xié)助

一、所需工具軟件

1. VS, Qt

2. C++

二、使用步驟

1.引入庫

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

2. 代碼實現(xiàn)

代碼如下:

#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));

    //內(nèi)層漸變
    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. 運行結(jié)果

?Qt+C++自定義控件儀表盤動畫仿真,C++,qt,c++,開發(fā)語言,visual studio,仿真

Qt+C++自定義控件儀表盤動畫仿真,C++,qt,c++,開發(fā)語言,visual studio,仿真

三、在線協(xié)助:

如需安裝運行環(huán)境或遠程調(diào)試,見文章底部個人 QQ 名片,由專業(yè)技術(shù)人員遠程協(xié)助!
1)遠程安裝運行環(huán)境,代碼調(diào)試
2)Qt, C++, Python入門指導(dǎo)
3)界面美化
4)軟件制作

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

博主推薦文章:python人臉識別統(tǒng)計人數(shù)qt窗體-CSDN博客

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

? ? ? ? ? ? ? ? ? ? ? ? ?Python OpenCV識別行人入口進出人數(shù)統(tǒng)計_python識別人數(shù)-CSDN博客

個人博客主頁:alicema1111的博客_CSDN博客-Python,C++,網(wǎng)頁領(lǐng)域博主

博主所有文章點這里alicema1111的博客_CSDN博客-Python,C++,網(wǎng)頁領(lǐng)域博主文章來源地址http://www.zghlxwxcb.cn/news/detail-650046.html

到了這里,關(guān)于Qt+C++自定義控件儀表盤動畫仿真的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 界面控件DevExpress WPF Gauge組件 - 輕松實現(xiàn)個性化商業(yè)儀表盤

    界面控件DevExpress WPF Gauge組件 - 輕松實現(xiàn)個性化商業(yè)儀表盤

    DevExpress WPF Gauge(儀表)控件包含了多種圓形儀表類型、水平和垂直線性儀表、分段和矩陣數(shù)字儀表以及狀態(tài)指示器,同時還具有最終用戶交互性的集成支持。 P.S :DevExpress WPF擁有120+個控件和庫,將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務(wù)應(yīng)用程序。通過DevExpress WPF能

    2024年02月06日
    瀏覽(23)
  • QPaint繪制自定義儀表盤組件01

    QPaint繪制自定義儀表盤組件01

    網(wǎng)上抄別人的,只是放這里自己看一下,看完就刪掉 ui Dashboard.pro ?mainwindow.h ?mainwindow.cpp main.cpp?

    2024年02月22日
    瀏覽(19)
  • WordPress后臺儀表盤自定義添加刪除概覽項目插件Glance That

    WordPress后臺儀表盤自定義添加刪除概覽項目插件Glance That

    成功搭建WordPress站點,登錄后臺后可以在“儀表盤 – 概覽”中看到包括多少篇文章、多少個頁面、多少條評論和當(dāng)前WordPress版本號及所使用的主題。具體如下圖所示: 但是如果我們的WordPress站點還有自定義文章類型,也想在概覽中顯示出來應(yīng)該怎么做呢?我還想要在概覽中

    2024年01月18日
    瀏覽(20)
  • Qt —— 自定義飛機儀表控件(附源碼)

    Qt —— 自定義飛機儀表控件(附源碼)

    示例效果 ? 部署環(huán)境 ?????本人親測版本Vs2017+Qt5.12.4,其他版本應(yīng)該也可使用。 ? 源碼1

    2024年01月25日
    瀏覽(18)
  • 新版Grafana儀表盤

    新版Grafana儀表盤

    一 Grafana 是什么 ????????Grafana 是一個開源的指標量監(jiān)測和可視化工具,常用于展示基礎(chǔ)設(shè)施的時序數(shù)據(jù)和應(yīng)用 程序運行分析。 ????????官網(wǎng)指路: https://grafana.com/ ????????與前文相關(guān)的兩個概念: ????????1)數(shù)據(jù)源(Datasource):定義了將用方式來查詢數(shù)據(jù)展

    2024年02月13日
    瀏覽(25)
  • echarts繪制儀表盤

    echarts繪制儀表盤

    ?代碼展示:

    2024年02月13日
    瀏覽(26)
  • Grafana增加儀表盤

    Grafana增加儀表盤

    grafana 是一款采用Go語言編寫的開源應(yīng)用,主要用于大規(guī)模指標數(shù)據(jù)的可視化展現(xiàn),是網(wǎng)絡(luò)架構(gòu)和應(yīng)用分析中最流行的時序數(shù)據(jù)展示工具,目前已經(jīng)支持絕大部分常用的時序數(shù)據(jù)庫。 Grafana下載地址:https://grafana.com/grafana/download Grafana儀表盤模板下載地址:https://grafana.com/grafa

    2024年02月04日
    瀏覽(21)
  • ChatGPT實現(xiàn)儀表盤生成

    ChatGPT實現(xiàn)儀表盤生成

    Grafana是開源社區(qū)最流行的數(shù)據(jù)可視化軟件,一定程度上也和 superset 一起被視為 tableau 等商業(yè) BI 的開源替代品,很多IT 團隊、科研團隊,都會使用 Grafana 來做數(shù)據(jù)監(jiān)控、挖掘分析。Grafana社區(qū)也有很多貢獻者,在 github 上分享自己針對不同場景制作的數(shù)據(jù)分析儀表盤效果和配置

    2024年02月02日
    瀏覽(17)
  • QML 儀表盤小示例

    QML 儀表盤小示例

    本次項目已發(fā)布在CSDN-GitCode,下載方便,安全,可在我主頁進行下載即可,后面的項目和素材都會發(fā)布這個平臺。 個人主頁:https://gitcode.com/user/m0_45463480 怎么下載:在項目中點擊克隆,windows:zip linux:tar.gz tar #?.pro

    2024年02月05日
    瀏覽(19)
  • Prometheus + Grafana 搭建監(jiān)控儀表盤

    Prometheus + Grafana 搭建監(jiān)控儀表盤

    目標要求 1、需要展現(xiàn)的儀表盤: SpringBoot或JVM儀表盤 Centos物理機服務(wù)器(實際為物理分割的虛擬服務(wù)器)儀表盤 2、展現(xiàn)要求: 探索Prometheus + Grafana搭建起來的展示效果,盡可能展示能展示的部分。 監(jiān)控系統(tǒng)核心:prometheus-2.45.0.linux-amd64.tar 下載地址:https://github.com/prometheus

    2024年04月23日
    瀏覽(31)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包