Qt自定义精美的仪表盘控件(汽车仪表、指南针、雷达、摇杆)

本文主要是介绍Qt自定义精美的仪表盘控件(汽车仪表、指南针、雷达、摇杆),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、网速测速仪表盘

在这里插入图片描述

#include "Frm_GaugeSpeed.h"
#include <QTimer>
#include <QPainter>
#include <QtMath>
#include <QDebug>//QString qss1 = QString("QLabel{background-color:rgb(0,0,0);color:rgb(%1);}").arg("100,184,255");
//QString qss2 = QString("QLabel{background-color:rgb(0,0,0);color:rgb(%1);}").arg("255,107,107");
//QString qss3 = QString("QLabel{background-color:rgb(0,0,0);color:rgb(%1);}").arg("24,189,155");//===================================================================================
// 函数名称 | Frm_GaugeSpeed()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |构造函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
Frm_GaugeSpeed::Frm_GaugeSpeed(QWidget *parent) : QWidget(parent)
{m_timer = new QTimer(this);m_timer->setInterval(1000);m_timer->start();connect(m_timer, SIGNAL(timeout()), this, SLOT(slotAnimaValue()));if(this->m_isAnimation == true){QTimer::singleShot(50, this, SLOT(slotUpdateValue()));}}
//===================================================================================
// 函数名称 | ~Frm_GaugeSpeed()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |析构函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
Frm_GaugeSpeed::~Frm_GaugeSpeed()
{}//===================================================================================
// 函数名称 | drawScale()
//-----------------------------------------------------------------------------------
// 函数输入 |painter画家
//-----------------------------------------------------------------------------------
// 函数功能 | 绘制刻度线
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_GaugeSpeed::drawScale(QPainter *painter)
{int radius = 94;painter->save();QPen pen;pen.setCapStyle(Qt::RoundCap);painter->rotate(m_IntSartAngle);int steps = (m_IntScaleMajor * m_IntScaleMinor);double angleStep = (360.0 - m_IntSartAngle - m_IntEndAngle) / steps;//计算圆环对应大刻度范围索引int indexStart = steps * (double)m_IntRingStartPercent / 100 + 1;int indexMid = steps * (double)m_IntRingMidPercent / 100 - 1;int indexEnd = steps * (double)m_IntRingEndPercent / 100 + 1;int index = 0;for (int i = 0; i <= steps; i++){if (i % m_IntScaleMinor == 0){//根据所在圆环范围切换颜色if (index < indexStart) {pen.setColor(m_ColorRingStart);} else if (index < (indexStart + indexMid)) {pen.setColor(m_ColorRingMid);} else if (index < (indexStart + indexMid + indexEnd)) {pen.setColor(m_ColorRingEnd);}index++;pen.setWidthF(1.5);painter->setPen(pen);painter->drawLine(0, radius - 13, 0, radius);} else {pen.setWidthF(0.5);pen.setColor(QColor(0,255,255));painter->setPen(pen);painter->drawLine(0, radius - 5, 0, radius);}painter->rotate(angleStep);}painter->restore();
}//===================================================================================
// 函数名称 | drawPointer()
//-----------------------------------------------------------------------------------
// 函数输入 |painter画家
//-----------------------------------------------------------------------------------
// 函数功能 | 绘制指针
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_GaugeSpeed::drawPointer(QPainter *painter)
{int radius = 62;painter->save();painter->setPen(Qt::NoPen);painter->setBrush(m_pointerColor);QPolygon pts;pts.setPoints(4, -5, 0, 0, -8, 5, 0, 0, radius);painter->rotate(m_IntSartAngle);double degRotate = (360.0 - m_IntSartAngle - m_IntEndAngle) / (m_dMaxValue - m_dMinValue) * (m_dCurrentValue - m_dMinValue);painter->rotate(degRotate);painter->drawConvexPolygon(pts);painter->restore();
}double Frm_GaugeSpeed::getMinValue() const
{return this->m_dMinValue;
}double Frm_GaugeSpeed::getMaxValue() const
{return this->m_dMaxValue;
}double Frm_GaugeSpeed::getValue() const
{return this->m_dCurrentValue;
}int Frm_GaugeSpeed::getPrecision() const
{return this->m_IntPreciBit;
}int Frm_GaugeSpeed::getScaleMajor() const
{return this->m_IntScaleMajor;
}int Frm_GaugeSpeed::getScaleMinor() const
{return this->m_IntScaleMinor;}int Frm_GaugeSpeed::getStartAngle() const
{return this->m_IntSartAngle;
}int Frm_GaugeSpeed::getEndAngle() const
{return this->m_IntEndAngle;
}bool Frm_GaugeSpeed::getAnimation() const
{return this->m_isAnimation;
}double Frm_GaugeSpeed::getAnimationStep() const
{return this->m_dAnimationStep;
}int Frm_GaugeSpeed::getRingWidth() const
{return  this->m_IntRingWidth;
}int Frm_GaugeSpeed::getRingStartPercent() const
{return this->m_IntRingStartPercent;
}int Frm_GaugeSpeed::getRingMidPercent() const
{return this->m_IntRingMidPercent;
}int Frm_GaugeSpeed::getRingEndPercent() const
{return  this->m_IntRingEndPercent;
}QColor Frm_GaugeSpeed::getRingColorStart() const
{return this->m_ColorRingStart;
}QColor Frm_GaugeSpeed::getRingColorMid() const
{return this->m_ColorRingMid;
}QColor Frm_GaugeSpeed::getRingColorEnd() const
{return this->m_ColorRingEnd;
}QColor Frm_GaugeSpeed::getPointerColor() const
{return this->m_pointerColor;
}QColor Frm_GaugeSpeed::getTextColor() const
{return this->m_textColor;
}QSize Frm_GaugeSpeed::sizeHint() const
{int width = this->width();int height = this->height();return QSize(qMin(width, height), qMin(width, height));
}QSize Frm_GaugeSpeed::minimumSizeHint() const
{int width = this->width();int height = this->height();return QSize(qMin(width, height), qMin(width, height));
}void Frm_GaugeSpeed::setRange(double minValue, double maxValue)
{this->m_dMinValue = minValue;this->m_dMaxValue = maxValue;}void Frm_GaugeSpeed::setRange(int minValue, int maxValue)
{this->m_dMinValue = minValue;this->m_dMaxValue = maxValue;}void Frm_GaugeSpeed::setValue(double value)
{this->m_dTargetValue = value;if(this->m_isAnimation == true){QTimer::singleShot(5, this, SLOT(slotUpdateValue()));}else {this->m_dCurrentValue = this->m_dTargetValue;}update();}void Frm_GaugeSpeed::setValue(int value)
{this->m_dTargetValue =value;if(this->m_isAnimation == true){QTimer::singleShot(5, this, SLOT(slotUpdateValue()));}else {this->m_dCurrentValue = this->m_dTargetValue;}update();
}void Frm_GaugeSpeed::setValue(QString &value)
{this->m_dTargetValue = value.toDouble();if(this->m_isAnimation == true){QTimer::singleShot(5, this, SLOT(slotUpdateValue()));}else {this->m_dCurrentValue = this->m_dTargetValue;}update();
}void Frm_GaugeSpeed::setAnimation(bool animation)
{this->m_isAnimation = animation;if(this->m_isAnimation == true){QTimer::singleShot(5, this, SLOT(slotUpdateValue()));}}void Frm_GaugeSpeed::slotUpdateValue()
{if(this->m_isAnimation == true){if(fabs(m_dCurrentValue - m_dTargetValue) > 0.001){if(m_dCurrentValue - m_dTargetValue > 0.001)m_dCurrentValue = m_dCurrentValue - m_dAnimationStep;else {m_dCurrentValue = m_dCurrentValue + m_dAnimationStep;}QTimer::singleShot(5, this, SLOT(slotUpdateValue()));update();}}}//===================================================================================
// 函数名称 | slotAnimaValue()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 | 模拟计数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_GaugeSpeed::slotAnimaValue()
{setValue(m_dCurrentValue+m_dAnimationStep);}

二、汽车仪表盘

在这里插入图片描述

#include "Frm_Dashboard.h"
#include <QTimer>
#include <QPainter>
#include <QtMath>
#include <QDebug>//===================================================================================
// 函数名称 | Frm_Dashboard()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |构造函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
Frm_Dashboard::Frm_Dashboard(QWidget *parent) : QWidget(parent)
{m_timer = new QTimer(this);m_timer->setInterval(1000);m_timer->start();connect(m_timer, SIGNAL(timeout()), this, SLOT(slotAnimaValue()));if(this->m_isAnimation == true){QTimer::singleShot(50, this, SLOT(slotUpdateValue()));}}
//===================================================================================
// 函数名称 | ~Frm_Dashboard()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |析构函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
Frm_Dashboard::~Frm_Dashboard()
{}double Frm_Dashboard::getMinValue() const
{return this->m_dMinValue;
}double Frm_Dashboard::getMaxValue() const
{return this->m_dMaxValue;
}double Frm_Dashboard::getValue() const
{return this->m_dCurrentValue;
}int Frm_Dashboard::getPrecision() const
{return this->m_IntPreciBit;
}int Frm_Dashboard::getScaleMajor() const
{return this->m_IntScaleMajor;
}int Frm_Dashboard::getScaleMinor() const
{return this->m_IntScaleMinor;}int Frm_Dashboard::getStartAngle() const
{return this->m_IntSartAngle;
}int Frm_Dashboard::getEndAngle() const
{return this->m_IntEndAngle;
}bool Frm_Dashboard::getAnimation() const
{return this->m_isAnimation;
}double Frm_Dashboard::getAnimationStep() const
{return this->m_dAnimationStep;
}QColor Frm_Dashboard::getPointerColor() const
{return this->m_pointerColor;
}QColor Frm_Dashboard::getTextColor() const
{return this->m_textColor;
}QSize Frm_Dashboard::sizeHint() const
{int width = this->width();int height = this->height();return QSize(qMin(width, height), qMin(width, height));
}QSize Frm_Dashboard::minimumSizeHint() const
{int width = this->width();int height = this->height();return QSize(qMin(width, height), qMin(width, height));
}//===================================================================================
// 函数名称 | paintEvent()
//-----------------------------------------------------------------------------------
// 函数输入 |painter画家
//-----------------------------------------------------------------------------------
// 函数功能 |绘制事件
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_Dashboard::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 / 200.0, side / 200.0); /* 比例缩放 */drawRing(&painter);drawScale(&painter);drawScaleNum(&painter);drawOverlay(&painter);drawPointer(&painter);drawValue(&painter);
}
//===================================================================================
// 函数名称 | drawRing()
//-----------------------------------------------------------------------------------
// 函数输入 |painter画家
//-----------------------------------------------------------------------------------
// 函数功能 | 绘制外围环
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_Dashboard::drawRing(QPainter *painter)
{int r = m_Radius;painter->save();painter->setPen(Qt::NoPen);painter->setBrush(QColor(172, 172, 172));//外围painter->drawEllipse(-r, -r, r * 2, r * 2);r =  m_Radius * 0.9;painter->setBrush(QColor(0, 26, 51));//内圆painter->setPen(Qt::NoPen);painter->drawEllipse(-r, -r, r * 2, r * 2);painter->restore();
}

三、指南针表盘

在这里插入图片描述

#include "Frm_Compass.h"
#include <QTimer>
#include <QPainter>
#include <QtMath>
#include <QDebug>//===================================================================================
// 函数名称 | Frm_Compass()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |构造函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
Frm_Compass::Frm_Compass(QWidget *parent) : QWidget(parent)
{m_timer = new QTimer(this);m_timer->setInterval(1000);m_timer->start();connect(m_timer, SIGNAL(timeout()), this, SLOT(slotAnimaValue()));if(this->m_isAnimation == true){QTimer::singleShot(50, this, SLOT(slotUpdateValue()));}}
//===================================================================================
// 函数名称 | ~Frm_Compass()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |析构函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
Frm_Compass::~Frm_Compass()
{}double Frm_Compass::getValue() const
{return this->m_dCurrentValue;
}int Frm_Compass::getPrecision() const
{return this->m_IntPreciBit;
}bool Frm_Compass::getAnimation() const
{return this->m_isAnimation;
}double Frm_Compass::getAnimationStep() const
{return this->m_dAnimationStep;
}QColor Frm_Compass::getTextColor() const
{return this->m_textColor;
}QSize Frm_Compass::sizeHint() const
{int width = this->width();int height = this->height();return QSize(qMin(width, height), qMin(width, height));
}QSize Frm_Compass::minimumSizeHint() const
{int width = this->width();int height = this->height();return QSize(qMin(width, height), qMin(width, height));
}//===================================================================================
// 函数名称 | paintEvent()
//-----------------------------------------------------------------------------------
// 函数输入 |painter画家
//-----------------------------------------------------------------------------------
// 函数功能 |绘制事件
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_Compass::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 / 200.0, side / 200.0);//绘制外边框圆drawRing(&painter);//绘制背景圆drawBgCircle(&painter);//绘制刻度drawScale(&painter);//绘制东南西北标识drawScaleNum(&painter);//绘制覆盖圆外圆drawCoverOuterCircle(&painter);//绘制覆盖圆内圆drawCoverInnerCircle(&painter);//绘制覆盖圆中心圆drawCoverCenterCircle(&painter);//绘制南北指针drawPointer(&painter);//绘制中心圆drawCenterCircle(&painter);//绘制当前值drawValue(&painter);//绘制遮罩drawOverlay(&painter);
}//==================================================================================================
//| 函数名称 | drawOverlay()
//|-------------------------------------------------------------------------------------------------
//| 函数功能 | 绘制遮罩层
//|-------------------------------------------------------------------------------------------------
//| 输入参数 |painter画家
//|-------------------------------------------------------------------------------------------------
//| 返回参数 |
//|-------------------------------------------------------------------------------------------------
//| 函数设计 | xt,2020.10.16
//==================================================================================================
void Frm_Compass::drawOverlay(QPainter *painter)
{int radius = 100;painter->save();painter->setPen(Qt::NoPen);QPainterPath smallCircle;QPainterPath bigCircle;radius -= 1;smallCircle.addEllipse(-radius, -radius, radius * 2, radius * 2);radius *= 2;bigCircle.addEllipse(-radius, -radius + 140, radius * 2, radius * 2);//高光的形状为小圆扣掉大圆的部分QPainterPath highlight = smallCircle - bigCircle;QLinearGradient linearGradient(0, -radius / 2, 0, 0);m_overlayColor.setAlpha(100);linearGradient.setColorAt(0.0, m_overlayColor);m_overlayColor.setAlpha(30);linearGradient.setColorAt(1.0, m_overlayColor);painter->setBrush(linearGradient);painter->rotate(-20);painter->drawPath(highlight);painter->restore();
}

四、钢铁侠雷达扫描

在这里插入图片描述

#include "Frm_Radar.h"#include <QPainter>
#include <QPaintEvent>
#include <QRadialGradient>
#include <QDebug>#include <time.h>
#include <ctime>
//===================================================================================
// 函数名称 | Frm_Radar()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |构造函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
Frm_Radar::Frm_Radar(QWidget *parent): QWidget(parent)
{m_radarAreaRect = QRect(-m_uinRadius + 10, -m_uinRadius + 10,2 * (m_uinRadius - 10), 2 * (m_uinRadius - 10));connect(&m_rotateTimer, &QTimer::timeout, this, &Frm_Radar::slotRotate);m_rotateTimer.setInterval(60);m_rotateTimer.start();m_scaleFont.setFamily("微软雅黑");m_scaleFont.setPixelSize(4);m_vecCheckPos = new std::vector<PosStruct>;m_vecCheckPos->reserve(10);
}
//===================================================================================
// 函数名称 | ~Frm_Radar()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |析构函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
Frm_Radar::~Frm_Radar()
{}//===================================================================================
// 函数名称 | paintEvent()
//-----------------------------------------------------------------------------------
// 函数输入 |painter画家
//-----------------------------------------------------------------------------------
// 函数功能 |绘制事件
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_Radar::paintEvent(QPaintEvent *event)
{Q_UNUSED(event);QPainter painter(this);int side = qMin(width(), height());int x = (width() - side) / 2;int y = (height() - side) / 2;// 设置ViewPortpainter.setViewport(QRect(x, y, side, side));// 设置窗口 逻辑坐标painter.setWindow(QRect(-m_uinRadius, -m_uinRadius, 2 * m_uinRadius, 2 * m_uinRadius));painter.setRenderHint(QPainter::Antialiasing);drawScale(painter);drawBackground(painter);drawScanArea(painter);for (auto item = m_vecCheckPos->begin(); item != m_vecCheckPos->end(); ++item){drawPoint(painter, *item);}
}
//===================================================================================
// 函数名称 | drawScan()
//-----------------------------------------------------------------------------------
// 函数输入 |painter画家
//-----------------------------------------------------------------------------------
// 函数功能 |绘制雷达刻度线
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_Radar::drawScale(QPainter &painter)
{painter.save();painter.setPen(QPen(QColor("#2252E1"), 0));painter.setFont(m_scaleFont);qreal radius = m_uinRadius - 10;int value = 15;for(int i = 0; i < 24; i++){painter.rotate(360 / 24);painter.drawText(QPoint(0, -radius - 4), value == 360 ? "0" : QString::number(value));value += 15;}for(int i = 0; i < 120; i++){painter.rotate(3);//旋转3°if(i % 5 == 0)painter.drawLine(QPoint(0, radius - 5), QPoint(0, radius));elsepainter.drawLine(QPoint(0, radius - 2), QPoint(0, radius));}painter.restore();
}
//===================================================================================
// 函数名称 | drawBackground()
//-----------------------------------------------------------------------------------
// 函数输入 |painter画家
//-----------------------------------------------------------------------------------
// 函数功能 |绘画雷达区域背景
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_Radar::drawBackground(QPainter &painter)
{painter.save();// 画螺旋环painter.setBrush(Qt::NoBrush);painter.setPen(QPen(QColor("#29EE47"), 0));painter.setCompositionMode(QPainter::CompositionMode_Multiply);for (unsigned int i = 0; i < m_uinRadius; i = i + 10){painter.drawEllipse(QPoint(0, 0), i, i);}// 画背景QRadialGradient radialGradient(QPoint(0, 0), m_uinRadius, QPoint(0, 0));radialGradient.setColorAt(0, QColor("#1B47EA"));radialGradient.setColorAt(1, QColor("#2DE3E9"));painter.setBrush(radialGradient);painter.drawEllipse(m_radarAreaRect);painter.restore();
}
//===================================================================================
// 函数名称 | drawScanArea()
//-----------------------------------------------------------------------------------
// 函数输入 |painter画家
//-----------------------------------------------------------------------------------
// 函数功能 |绘制雷达扫描区域
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_Radar::drawScanArea(QPainter &painter)
{// 画扫描区域painter.save();painter.setCompositionMode(QPainter::CompositionMode_SourceOver);painter.rotate(m_fRotateAngle);QColor color_0("#47D72D"); color_0.setAlpha(200);QColor color_2("#47D72D"); color_2.setAlpha(0);//绘制扫描线painter.setPen(QPen(color_0, 0));painter.drawLine(QPoint(0, 0), QPoint(m_uinRadius - 10, 0));//绘制扫描线拖尾QConicalGradient scanAreaGradient(QPoint(0, 0), 0);scanAreaGradient.setColorAt(0.0, color_0);scanAreaGradient.setColorAt(1. / 360, color_0);scanAreaGradient.setColorAt(40. / 360, color_2);painter.setPen(Qt::NoPen);painter.setBrush(scanAreaGradient);painter.drawPie(m_radarAreaRect, 0, 40 * 16);painter.restore();
}

五、全方位雷达扫描

在这里插入图片描述

#include "Frm_ScanTanTan1.h"
#include <QTimer>
#include <QPainter>
#include <QtMath>
#include <QDebug>
#include <QPropertyAnimation>
#include <QSequentialAnimationGroup>//===================================================================================
// 函数名称 | Frm_ScanTanTan2()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |构造函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
Frm_ScanTanTan1::Frm_ScanTanTan1(QWidget *parent) : QWidget(parent)
{//监测点列表m_vecCheckPos = new std::vector<PosStruct>;m_vecCheckPos->reserve(10);//加载图片setImage(QPixmap("./Resources/image/IronMan2.jpeg"));//动画QPropertyAnimation *anim1 = new QPropertyAnimation(this, "");anim1->setStartValue(40);anim1->setEndValue(50);anim1->setDuration(300);QPropertyAnimation *anim2 = new QPropertyAnimation(this, "");anim2->setStartValue(50);anim2->setEndValue(30);anim2->setDuration(300);QPropertyAnimation *anim3 = new QPropertyAnimation(this, "");anim3->setStartValue(30);anim3->setEndValue(40);anim3->setDuration(300);animationGroup = new QSequentialAnimationGroup() ;animationGroup->addAnimation(anim1);animationGroup->addAnimation(anim2);animationGroup->addAnimation(anim3);connect(anim1, SIGNAL(valueChanged(QVariant)), this, SLOT(slotUpdateImage(QVariant)));connect(anim2, SIGNAL(valueChanged(QVariant)), this, SLOT(slotUpdateImage(QVariant)));connect(anim3, SIGNAL(valueChanged(QVariant)), this, SLOT(slotUpdateImage(QVariant)));//每隔10msec更新扫描线m_timer = new QTimer(this);m_timer->setInterval(10);m_timer->start();connect(m_timer, SIGNAL(timeout()), this, SLOT(slotChangeScan()));//每隔10msec更新扩散圈m_ringsTimer = new QTimer(this);m_ringsTimer->setInterval(100);m_ringsTimer->start();connect(m_ringsTimer, SIGNAL(timeout()), this, SLOT(slotChangeRing()));//每隔60ms更新监测点m_checkPointTimer = new QTimer(this);m_checkPointTimer->setInterval(60);m_checkPointTimer->start();connect(m_checkPointTimer,SIGNAL(timeout()), this, SLOT(slotRotatePoint()));}
//===================================================================================
// 函数名称 | ~Frm_ScanTanTan2()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |析构函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
Frm_ScanTanTan1::~Frm_ScanTanTan1()
{if(m_vecCheckPos){delete m_vecCheckPos;m_vecCheckPos = NULL;}}
//===================================================================================
// 函数名称 | mousePressEvent()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |鼠标按下事件
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_ScanTanTan1::mousePressEvent(QMouseEvent *event)
{if(!mb_isPressed){QPoint pressedPoint =  event->pos();QPointF realPos = pressedPoint - rect().center();QMatrix qMatrix;int width = this->width();int height = this->height();int side = qMin(width, height);qMatrix.scale(side / 200.0, side / 200.0);QRegion mEllipse = QRegion(-m_uinRingRadius, -m_uinRingRadius, m_uinRingRadius * 2, m_uinRingRadius * 2, QRegion::Ellipse);mEllipse = qMatrix.map(mEllipse);if( mEllipse.contains(realPos.toPoint())){mb_isPressed = true;animationGroup->stop();update();}}}
//===================================================================================
// 函数名称 | mouseReleaseEvent()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |鼠标松开事件
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_ScanTanTan1::mouseReleaseEvent(QMouseEvent *)
{if(mb_isPressed){mb_isPressed = false;RingData ring;ring.uin_radius = m_uinRingRadius;ring.uin_alpha = 255;ring.f_width = 3;m_listRings.push_back(ring);animationGroup->start();update();}}
//===================================================================================
// 函数名称 | paintEvent()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |绘制函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_ScanTanTan1::paintEvent(QPaintEvent *)
{int width = this->width();int height = this->height();int side = qMin(width, height);//绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放QPainter painter(this);painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);//圆滑处理斜线painter.translate(width / 2, height / 2);painter.scale(side / 200.0, side / 200.0);//绘制扫描线drawScan(&painter);//绘制扩散圈drawRing(&painter);//绘制中间图片drawImage(&painter);//绘制文本drawValue(&painter);//绘制监测点for (auto item = m_vecCheckPos->begin(); item != m_vecCheckPos->end(); ++item){drawPoint(painter, *item);}}

六、摇杆方向盘

在这里插入图片描述

#include "Frm_Rocker.h"
#include <QTimer>
#include <QPainter>
#include <QtMath>
#include <QDebug>
#include <QPropertyAnimation>
#include <QSequentialAnimationGroup>//===================================================================================
// 函数名称 | Frm_ScanTanTan2()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |构造函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
Frm_Rocker::Frm_Rocker(QWidget *parent) : QWidget(parent)
{m_timer = new QTimer(this);m_intMouseX = 0;m_intMouseY = 0;connect(m_timer,&QTimer::timeout,this,[=]{emit sigKeyNumchanged(getKeyNum());});connect(this,&Frm_Rocker::sigKeyNumchanged,this,[=](int num){ qDebug("当前位置=%d",num); });}
//===================================================================================
// 函数名称 | ~Frm_ScanTanTan2()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |析构函数
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
Frm_Rocker::~Frm_Rocker()
{}
//===================================================================================
// 函数名称 | mousePressEvent()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |鼠标按下事件
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_Rocker::mousePressEvent(QMouseEvent *event)
{m_intMouseX = event->pos().x() - width() / 2;m_intMouseY = event->pos().y() - height() / 2;qDebug("m_intMouseX=%d,m_intMouseY=%d",m_intMouseX,m_intMouseY);m_timer->start(100);mb_isPressed=true;update();
}
//===================================================================================
// 函数名称 | mouseReleaseEvent()
//-----------------------------------------------------------------------------------
// 函数输入 |
//-----------------------------------------------------------------------------------
// 函数功能 |鼠标松开事件
//-----------------------------------------------------------------------------------
// 返回值为 |
//-----------------------------------------------------------------------------------
// 函数作者 |xt
//===================================================================================
void Frm_Rocker::mouseReleaseEvent(QMouseEvent *)
{m_intMouseX = 0;//width()/2;m_intMouseY = 0;//height()/2;m_timer->stop();mb_isPressed=false;emit sigKeyNumchanged(Rock_Stop);update();
}

这篇关于Qt自定义精美的仪表盘控件(汽车仪表、指南针、雷达、摇杆)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

SpringBoot 自定义消息转换器使用详解

《SpringBoot自定义消息转换器使用详解》本文详细介绍了SpringBoot消息转换器的知识,并通过案例操作演示了如何进行自定义消息转换器的定制开发和使用,感兴趣的朋友一起看看吧... 目录一、前言二、SpringBoot 内容协商介绍2.1 什么是内容协商2.2 内容协商机制深入理解2.2.1 内容

python与QT联合的详细步骤记录

《python与QT联合的详细步骤记录》:本文主要介绍python与QT联合的详细步骤,文章还展示了如何在Python中调用QT的.ui文件来实现GUI界面,并介绍了多窗口的应用,文中通过代码介绍... 目录一、文章简介二、安装pyqt5三、GUI页面设计四、python的使用python文件创建pytho

QT实现TCP客户端自动连接

《QT实现TCP客户端自动连接》这篇文章主要为大家详细介绍了QT中一个TCP客户端自动连接的测试模型,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录版本 1:没有取消按钮 测试效果测试代码版本 2:有取消按钮测试效果测试代码版本 1:没有取消按钮 测试效果缺陷:无法手动停

基于Qt实现系统主题感知功能

《基于Qt实现系统主题感知功能》在现代桌面应用程序开发中,系统主题感知是一项重要的功能,它使得应用程序能够根据用户的系统主题设置(如深色模式或浅色模式)自动调整其外观,Qt作为一个跨平台的C++图形用... 目录【正文开始】一、使用效果二、系统主题感知助手类(SystemThemeHelper)三、实现细节

Qt实现文件的压缩和解压缩操作

《Qt实现文件的压缩和解压缩操作》这篇文章主要为大家详细介绍了如何使用Qt库中的QZipReader和QZipWriter实现文件的压缩和解压缩功能,文中的示例代码简洁易懂,需要的可以参考一下... 目录一、实现方式二、具体步骤1、在.pro文件中添加模块gui-private2、通过QObject方式创建

Qt QWidget实现图片旋转动画

《QtQWidget实现图片旋转动画》这篇文章主要为大家详细介绍了如何使用了Qt和QWidget实现图片旋转动画效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、效果展示二、源码分享本例程通过QGraphicsView实现svg格式图片旋转。.hpjavascript

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

【专题】2024飞行汽车技术全景报告合集PDF分享(附原数据表)

原文链接: https://tecdat.cn/?p=37628 6月16日,小鹏汇天旅航者X2在北京大兴国际机场临空经济区完成首飞,这也是小鹏汇天的产品在京津冀地区进行的首次飞行。小鹏汇天方面还表示,公司准备量产,并计划今年四季度开启预售小鹏汇天分体式飞行汽车,探索分体式飞行汽车城际通勤。阅读原文,获取专题报告合集全文,解锁文末271份飞行汽车相关行业研究报告。 据悉,业内人士对飞行汽车行业