当前位置: 首页 > news >正文

Qt绘制温度计源码分享

Qt绘制温度计源码分享

  • 一、效果展示
  • 二、源码分享
  • 三、使用方法

一、效果展示

在这里插入图片描述
在这里插入图片描述

二、源码分享

.hpp

#ifndef THERMOMETERWIDGET_H
#define THERMOMETERWIDGET_H#include <QWidget>
#include <QPropertyAnimation>
#include <QPainter>
#include <QTimer>class ThermometerWidget : public QWidget
{Q_OBJECT
signals:
public:explicit ThermometerWidget(QWidget *parent = nullptr);qreal getValue();void setValue(qreal value);void changeValue(qreal value);
protected:void paintEvent(QPaintEvent *e);
public slots:void startAnimation();
private:void updateRect();
private:qreal m_value;qreal curValue;int m_width;QRectF m_rect;int maxValue, minValue;qreal m_radius;QPropertyAnimation *m_valueAnimation;};#endif // THERMOMETERWIDGET_H

.cpp

#include "thermometerWidget.h"ThermometerWidget::ThermometerWidget(QWidget *parent): QWidget{parent}
{m_width = 20;maxValue = 100;minValue = -20;m_radius = 1.05;m_value = 0;curValue = m_value;QTimer *at = new QTimer(this);at->start(1000);// m_valueAnimation = new QPropertyAnimation(this, "value");// m_valueAnimation->setDuration(1000);// m_valueAnimation->setEasingCurve(QEasingCurve::OutCubic);// m_valueAnimation->setLoopCount(1);connect(at, SIGNAL(timeout()), this, SLOT(startAnimation()));
}qreal ThermometerWidget::getValue()
{return m_value;
}void ThermometerWidget::setValue(qreal value)
{m_value = value;update();
}void ThermometerWidget::changeValue(qreal value)
{if(value > maxValue)value = maxValue;if(value < minValue)value = minValue;curValue = value;
}void ThermometerWidget::paintEvent(QPaintEvent *e)
{updateRect();QPainter painter(this);QPen pen(Qt::black);painter.translate(this->width()/2, this->height()/2);  //坐标轴移动到中心点painter.setRenderHints(QPainter::TextAntialiasing | QPainter::Antialiasing);  // 启用反锯齿painter.save();//绘制上方的柱状painter.fillRect(m_rect, QColor(168,200, 225));//绘制底部的圆QRectF tmpRect = QRectF(m_rect.bottomLeft(), QPointF(m_width, height()/2- m_width*m_radius));painter.fillRect(tmpRect, QColor(255, 0, 0));painter.setPen(Qt::NoPen);painter.setBrush(QColor(255, 0, 0));painter.drawEllipse(tmpRect.bottomLeft()+QPointF(tmpRect.width()/2, 0),m_width*m_radius, m_width*m_radius);painter.restore();//绘制刻度以及刻度值painter.save();painter.setPen(QColor(Qt::black));int nYCount = (maxValue - minValue)/10+1;qreal perHeight = (m_rect.height())/nYCount;for (int i=0; i<nYCount; ++i) {QPointF basePoint = m_rect.bottomLeft() - QPointF(0, perHeight/2) - QPointF(0, perHeight*i);//左侧大刻度painter.drawLine(basePoint, basePoint+QPointF(-10, 0));for (int j=1; j<10; ++j) {if(i == nYCount -1)continue;painter.drawLine(basePoint-QPointF(0, perHeight/10*j),basePoint-QPointF(5, perHeight/10*j));}painter.drawText(basePoint+QPointF(-28, 4), QString("%1").arg(minValue+i*10));//右侧大刻度basePoint = m_rect.bottomRight() - QPointF(0, perHeight/2) - QPointF(0, perHeight*i);painter.drawLine(basePoint, basePoint+QPointF(10, 0));for (int j=1; j<10; ++j) {if(i == nYCount -1)continue;painter.drawLine(basePoint-QPointF(0, perHeight/10*j),basePoint-QPointF(-5, perHeight/10*j));}}painter.restore();//根据值填充m_rectqreal h = (m_value-minValue)/(maxValue-minValue)*(m_rect.height()-perHeight);if(h<0)h = 0;if(h > m_rect.height())h = m_rect.height();painter.fillRect(m_rect.adjusted(0, m_rect.height()-h-perHeight/2-1 , 0, 0), QColor(255, 0, 0));QWidget::paintEvent(e);
}void ThermometerWidget::startAnimation()
{// qreal startValue = getValue();// m_valueAnimation->setKeyValueAt(0, startValue-1);// m_valueAnimation->setKeyValueAt(0.5, curValue+1);// m_valueAnimation->setKeyValueAt(1, curValue);// m_valueAnimation->setStartValue(startValue-2);// m_valueAnimation->start();
}void ThermometerWidget::updateRect()
{m_rect.setX(0);m_rect.setY(20 - height()/2);m_rect.setWidth(m_width);m_rect.setHeight(height() - 40 - m_width* m_radius);
}

三、使用方法

放置一个QWidget提升为这个类,
在这里插入图片描述

相关文章:

  • Vulkan学习笔记1—环境搭建
  • jsoncpp ubuntu编译问题
  • 安卓9.0系统修改定制化____系列讲解导读篇
  • Pico Unity开发记录
  • 做好机房整改前的准备工作
  • 【开源解析】:Python打造专业级USB安全弹出工具(附完整源码)
  • 基于uniapp实现自定义日历页面、年份月份选择、动态日历渲染、日期标记及备忘录、无组件依赖、多端兼容
  • 构建高效开发节奏:我的IDEA休息提醒插件实践
  • uniapp请求接口封装
  • vue定义的组件在外部引入时的问题
  • Hadoop 2.7.7 单机伪分布式安装与配置教程(JDK 8)
  • LSTM-XGBoost回归预测,多输入单输出(Matlab完整源码和数据)
  • 洛谷B3612 【深进1.例1】求区间和
  • matlab脉冲信号并绘制波形2025.6.11
  • java每日精进 6.11【消息队列】
  • Python训练营打卡 Day51
  • Weblogic 弱口令-任意文件读取
  • 零基础设计模式——行为型模式 - 中介者模式
  • IDEA 连接 Docker 一键打镜像
  • k8s 部署服务常见错误原因
  • 合作社做网站有用吗/app推广拉新一手渠道
  • 网站怎么做反向代理/成都网站seo性价比高
  • wordpress ip修改/安卓系统优化大师
  • 做网站的公司经营范围怎么写/搭建网站
  • 网站的后台管理员系统建设教程/建网站不花钱免费建站
  • 东营网站设计/百度健康