一个实时波形图的封装demo(QT)(qcustomplot)

2024-02-29 08:28

本文主要是介绍一个实时波形图的封装demo(QT)(qcustomplot),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言:

        封装的一个实时波形图的类,可以直接提升使用。 提供了接口,可以更改颜色,样式,等等

参考:

Qt Plotting Widget QCustomPlot - Introduction

另外参考了一个大神的作品,链接没找到。

项目文件:

123盘  

123盘  实时波形图官方版下载丨最新版下载丨绿色版下载丨APP下载-123云盘

CSDN

【免费】一个实时波形图的封装demo(QT)(qcustomplot)资源-CSDN文库

源码

WaveChart文件夹:

1.加入文件qcustomplot.h 和 qcustomplot.cpp:

2.WaveChart.pri文件:

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupportFORMS += \$$PWD/waveWidget.uiHEADERS += \$$PWD/qcustomplot.h \$$PWD/waveWidget.hSOURCES += \$$PWD/qcustomplot.cpp \$$PWD/waveWidget.cpp

3.waveWidget.h文件:

#ifndef WAVEWIDGET_H
#define WAVEWIDGET_H#include <QWidget>
#include <QVBoxLayout>
#include <vector>
#include <cstring>
#include <numeric>
//#include <QScrollBar>
#include "qcustomplot.h"
using std::vector;
namespace Ui {
class WaveWidget;
}class WaveWidget : public QWidget
{Q_OBJECTpublic:explicit WaveWidget(QWidget *parent = nullptr);~WaveWidget();void init(bool isShowTicks=true, bool isShowTickLables=true, bool isShowGrid=true, bool isShowSubGrid=true, bool isShowAxis2=true, bool isGraphBigAndSmall=true);void setTheme(QColor axis, QColor background);void setXAxisLable(const char* name, const char* name2 = "");void setYAxisLable(const char* name, const char* name2 = "");void setPen(Qt::GlobalColor penColor, QCPGraph::LineStyle lineStyle = QCPGraph::lsLine, QColor brushColor = QColor(255,255, 255, 100), bool isShow = true);void addNewData(double data);void setTitle(const char* name);double getMax();double getMin();double getAverage();QCustomPlot*    getTheChartPlot();QCPGraph*       getTheGraph();vector<double>  &getTheVector();private:Ui::WaveWidget *ui;QCustomPlot*    chartPlot = new QCustomPlot(this);      // 图表QCPGraph*       graph;                                  // 曲线//QScrollBar      horizontalScrollBar;vector<double>  graphData;                              // 图表数据,只作记录使用int             timeRange = 10;                         // 时间轴范围QTime time;double          sum = 0.0;};#endif // WAVEWIDGET_H

4.waveWidget.cpp文件

#include "waveWidget.h"
#include "ui_waveWidget.h"WaveWidget::WaveWidget(QWidget *parent) :QWidget(parent),ui(new Ui::WaveWidget)
{ui->setupUi(this);// 初始化图表graph = chartPlot->addGraph();// graph->addData(1,1);// graph->addData(2,3);// 初始化设置// chartPlot->xAxis->setRange(1, 4096);// chartPlot->yAxis->setRange(-1, 350); // 假设灰度值的范围为0-255// chartPlot->xAxis->setLabel("Pixel");// chartPlot->yAxis->setLabel("Gray Value");QVBoxLayout* layout = new QVBoxLayout;layout->addWidget(ui->title);layout->addWidget(chartPlot);chartPlot->setSizePolicy(QSizePolicy::Policy(QSizePolicy::Preferred), QSizePolicy::Policy(QSizePolicy::Expanding));//layout->addWidget((QWidget)horizontalScrollBar);setLayout(layout);// init();}WaveWidget::~WaveWidget()
{delete ui;
}void WaveWidget::init(bool isShowTicks, bool isShowTickLables, bool isShowGrid, bool isShowSubGrid, bool isShowAxis2, bool isGraphBigAndSmall)
{time = QTime::currentTime();// 刻度显示chartPlot->xAxis->setTicks(isShowTicks);chartPlot->yAxis->setTicks(isShowTicks);// 刻度值显示chartPlot->xAxis->setTickLabels(isShowTickLables);chartPlot->yAxis->setTickLabels(isShowTickLables);// 网格显示chartPlot->xAxis->grid()->setVisible(isShowGrid);chartPlot->yAxis->grid()->setVisible(isShowGrid);// 子网格显示chartPlot->xAxis->grid()->setSubGridVisible(isShowSubGrid);chartPlot->yAxis->grid()->setSubGridVisible(isShowSubGrid);// 右和上坐标轴、刻度值显示chartPlot->xAxis2->setVisible(isShowAxis2);chartPlot->yAxis2->setVisible(isShowAxis2);chartPlot->yAxis2->setTicks(isShowAxis2);chartPlot->yAxis2->setTickLabels(isShowAxis2);// make top right axes clones of bottom left axes. Looks prettier:
//    chartPlot->axisRect()->setupFullAxesBox();// make left and bottom axes always transfer their ranges to right and top axes:if(isShowAxis2){connect(chartPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), chartPlot->xAxis2, SLOT(setRange(QCPRange)));connect(chartPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), chartPlot->yAxis2, SLOT(setRange(QCPRange)));}// 暗色主题//setPlotTheme(Qt::white, Qt::black);// 亮色主题setTheme(Qt::black, Qt::white);if(isGraphBigAndSmall){// 可放大缩小和移动chartPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);}// x轴以时间形式显示QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);timeTicker->setTimeFormat("%h:%m:%s");chartPlot->xAxis->setTicker(timeTicker);chartPlot->axisRect()->setupFullAxesBox();chartPlot->replot();
}void WaveWidget::setTheme(QColor axis, QColor background)
{
//----------------------------------------------------------------------------------------//// 坐标标注颜色chartPlot->xAxis->setLabelColor(axis);chartPlot->yAxis->setLabelColor(axis);// 坐标刻度值颜色chartPlot->xAxis->setTickLabelColor(axis);chartPlot->yAxis->setTickLabelColor(axis);// 坐标基线颜色和宽度chartPlot->xAxis->setBasePen(QPen(axis, 1));chartPlot->yAxis->setBasePen(QPen(axis, 1));// 坐标主刻度颜色和宽度chartPlot->xAxis->setTickPen(QPen(axis, 1));chartPlot->yAxis->setTickPen(QPen(axis, 1));// 坐标子刻度颜色和宽度chartPlot->xAxis->setSubTickPen(QPen(axis, 1));chartPlot->yAxis->setSubTickPen(QPen(axis, 1));// 坐标标注颜色chartPlot->xAxis2->setLabelColor(axis);chartPlot->yAxis2->setLabelColor(axis);// 坐标刻度值颜色chartPlot->xAxis2->setTickLabelColor(axis);chartPlot->yAxis2->setTickLabelColor(axis);// 坐标基线颜色和宽度chartPlot->xAxis2->setBasePen(QPen(axis, 1));chartPlot->yAxis2->setBasePen(QPen(axis, 1));// 坐标主刻度颜色和宽度chartPlot->xAxis2->setTickPen(QPen(axis, 1));chartPlot->yAxis2->setTickPen(QPen(axis, 1));// 坐标子刻度颜色和宽度chartPlot->xAxis2->setSubTickPen(QPen(axis, 1));chartPlot->yAxis2->setSubTickPen(QPen(axis, 1));// 整个画布背景色chartPlot->setBackground(background);// 绘图区域背景色chartPlot->axisRect()->setBackground(background);// 刷新绘图chartPlot->replot();
}void WaveWidget::setXAxisLable(const char *name, const char* name2)
{chartPlot->xAxis->setLabel(name);if(strlen(name2)!=0)chartPlot->xAxis2->setLabel(name2);chartPlot->replot();
}void WaveWidget::setYAxisLable(const char *name, const char* name2)
{chartPlot->yAxis->setLabel(name);if(strlen(name2)!=0)chartPlot->yAxis2->setLabel(name2);chartPlot->replot();
}void WaveWidget::setPen(Qt::GlobalColor penColor, QCPGraph::LineStyle lineStyle, QColor brushColor, bool isShow)
{graph->setPen(QPen(penColor)); // 设置线条颜色为蓝色graph->setLineStyle(lineStyle); // 设置线型为直线graph->setVisible(isShow);chartPlot->graph()->setBrush(QBrush(brushColor));chartPlot->replot();
}void WaveWidget::addNewData(double data)
{// 系统当前时间 = 系统运行初始时间 + 系统运行时间static double start = time.hour()*60*60 + time.minute()*60 + time.second() + time.msec()/1000.0;double key = start + time.elapsed()/1000.0;// 设置时间轴chartPlot->xAxis->setRange(key, timeRange, Qt::AlignRight);//chartPlot->rescaleAxes();// 刷新绘图水平滚动条//horizontalScrollBar.setRange(int(start), int(key));  // 刷新滚动条的范围//horizontalScrollBar.setPageStep(1);                  // 设置翻页步长为 1s 的宽度//horizontalScrollBar.setValue(int(key));              // 调整滑块位置到最右边// 更新曲线绘图chartPlot->graph()->addData(key, data);chartPlot->replot(QCustomPlot::rpQueuedReplot);// 存储曲线的当前值graphData.push_back(data);sum += data;
}void WaveWidget::setTitle(const char *name)
{ui->title->setText(name);
}double WaveWidget::getMax()
{return *std::max_element(graphData.begin(),graphData.end());
}double WaveWidget::getMin()
{return *std::min_element(graphData.begin(),graphData.end());
}double WaveWidget::getAverage()
{return 1.0*sum/(graphData.size());
}QCustomPlot *WaveWidget::getTheChartPlot()
{return   chartPlot;
}QCPGraph *WaveWidget::getTheGraph()
{return  graph;
}vector<double> &WaveWidget::getTheVector()
{return graphData;
}

5.测试demo:

新建测试界面:

提升为刚刚的类:

效果:

测试:

    ui->setupUi(this);// 初始化:可以设置参数ui->waveWidget->init();// 可选设置:ui->waveWidget->setXAxisLable("Time");ui->waveWidget->setYAxisLable("Value");ui->waveWidget->setPen(Qt::blue, QCPGraph::lsLine, QColor( 36,141,218,255));ui->waveWidget->setTheme(Qt::red, Qt::white);ui->waveWidget->setTitle("Wave Chart Test");// 测试加入数据:QTimer *timer = new QTimer(this);connect(timer,&QTimer::timeout,[=](){ui->waveWidget->addNewData((qrand()%1000)/1000.0);qDebug()<<"max:"<<ui->waveWidget->getMax()<<"   min:"<<ui->waveWidget->getMin()<<"  average:"<<ui->waveWidget->getAverage();if(num%10==0){qDebug()<<"size: "<<ui->waveWidget->getTheVector().size();ui->waveWidget->getTheVector().clear();qDebug()<<"size: "<<ui->waveWidget->getTheVector().size()<<"********";}if(num>50){timer->stop();}num++;});timer->start(100);

这篇关于一个实时波形图的封装demo(QT)(qcustomplot)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

Python用Flask封装API及调用详解

《Python用Flask封装API及调用详解》本文介绍Flask的优势(轻量、灵活、易扩展),对比GET/POST表单/JSON请求方式,涵盖错误处理、开发建议及生产环境部署注意事项... 目录一、Flask的优势一、基础设置二、GET请求方式服务端代码客户端调用三、POST表单方式服务端代码客户端调用四

Qt中实现多线程导出数据功能的四种方式小结

《Qt中实现多线程导出数据功能的四种方式小结》在以往的项目开发中,在很多地方用到了多线程,本文将记录下在Qt开发中用到的多线程技术实现方法,以导出指定范围的数字到txt文件为例,展示多线程不同的实现方... 目录前言导出文件的示例工具类QThreadQObject的moveToThread方法实现多线程QC

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

Qt QCustomPlot库简介(最新推荐)

《QtQCustomPlot库简介(最新推荐)》QCustomPlot是一款基于Qt的高性能C++绘图库,专为二维数据可视化设计,它具有轻量级、实时处理百万级数据和多图层支持等特点,适用于科学计算、... 目录核心特性概览核心组件解析1.绘图核心 (QCustomPlot类)2.数据容器 (QCPDataC

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

Qt如何实现文本编辑器光标高亮技术

《Qt如何实现文本编辑器光标高亮技术》这篇文章主要为大家详细介绍了Qt如何实现文本编辑器光标高亮技术,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录实现代码函数作用概述代码详解 + 注释使用 QTextEdit 的高亮技术(重点)总结用到的关键技术点应用场景举例示例优化建议

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro