QT绘制实时动态曲线

您所在的位置:网站首页 数据绘制曲线图 QT绘制实时动态曲线

QT绘制实时动态曲线

2024-07-13 10:20:29| 来源: 网络整理| 查看: 265

在QT绘制实时动态曲线——qcustomplot使用(一)中,介绍使用官方的demo实现动态实时曲线。

官方代码移植

针对动态显示数据项目要求,移植官方代码demo

开启绘图的函数 void Widget::setupRealtimeDataDemo(QCustomPlot *customPlot) { customPlot->addGraph(); // red line customPlot->graph(0)->setPen(QPen(Qt::red)); customPlot->graph(0)->setAntialiasedFill(true); customPlot->graph(0)->setName("VL压力"); customPlot->addGraph(); // darkYellow line customPlot->graph(1)->setPen(QPen(Qt::darkYellow)); customPlot->graph(1)->setAntialiasedFill(true); customPlot->graph(1)->setName("AC1压力"); customPlot->addGraph(); // black line customPlot->graph(2)->setPen(QPen(Qt::black)); customPlot->graph(2)->setAntialiasedFill(true); customPlot->graph(2)->setName("AC2压力"); customPlot->addGraph(); // green line customPlot->graph(3)->setPen(QPen(Qt::green)); customPlot->graph(3)->setAntialiasedFill(true); customPlot->graph(3)->setName("AS压力"); customPlot->addGraph(); // blue line customPlot->graph(4)->setName("SR压力"); customPlot->graph(4)->setPen(QPen(Qt::blue)); customPlot->graph(4)->setAntialiasedFill(true); customPlot->addGraph(); // magenta line customPlot->graph(5)->setPen(QPen(Qt::magenta)); customPlot->graph(5)->setAntialiasedFill(true); customPlot->graph(5)->setName("BC压力"); customPlot->xAxis->setLabel("Time(s)"); customPlot->yAxis->setLabel("压力(kpa)"); QSharedPointer timeTicker(new QCPAxisTickerDateTime); //时间日期作为X轴 timeTicker->setDateTimeFormat("dd.MM.yyyy hh:mm:ss"); customPlot->xAxis->setTicker(timeTicker); customPlot->xAxis->setRange(0, 1000); customPlot->axisRect()->setupFullAxesBox(); customPlot->yAxis->setRange(0, 1000); customPlot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom|QCP::iSelectPlottables);//可以缩放可以滚动点击选择 // make left and bottom axes transfer their ranges to right and top axes: connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange))); connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange))); // setup a timer that repeatedly calls MainWindow::realtimeDataSlot: connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot())); dataTimer.start(10); // Interval 0 means to refresh as fast as possible } 绘制图的槽函数 void Widget::realtimeDataSlot() { // calculate two new data points: double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;// time elapsed since start of demo, in seconds static double lastPointKey = 0; if (key-lastPointKey > 0.2) // at most add point every 2 ms { // add data to lines: customPlot->graph(0)->addData(key, rand()%700); customPlot->graph(1)->addData(key, rand()%700); customPlot->graph(2)->addData(key, rand()%500); customPlot->graph(3)->addData(key, rand()%400); customPlot->graph(4)->addData(key, rand()%250); customPlot->graph(5)->addData(key, rand()%100); lastPointKey = key; } customPlot->xAxis->setRange(key, 10, Qt::AlignRight); //设置x轴的范围 显示个数为8个 customPlot->replot(); // calculate frames per second: static double lastFpsKey; static int frameCount; ++frameCount; if (key-lastFpsKey > 2) // average fps over 2 seconds { lastFpsKey = key; frameCount = 0; } }

然后,在widget.h中添加一些变量的声明,在widget.c文件中new需要的新对象。

改动的内容如下:

1、绘图以当前时间日期为X轴,在官方的代码demo中进行修改。

QSharedPointer timeTicker(new QCPAxisTickerDateTime); //时间日期作为X轴 timeTicker->setDateTimeFormat("dd.MM.yyyy hh:mm:ss"); customPlot->xAxis->setTicker(timeTicker);

定义X轴的显示内容为当前时间!!!

double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;

2、 配置界面可以为缩放、可以滚动、可以选择

customPlot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom|QCP::iSelectPlottables);//可以缩放可以滚动点击选择

3、定时器开启间隔,之前的demo是0,没有修改之前刷新太快。

dataTimer.start(10);

效果如下: 在这里插入图片描述4、添加多条曲线

在这里插入图片描述 总结:之前使用Qchart绘图,线条数据变多,就发现界面发非常卡顿,现在使用qcustomplot绘图解决了这个问题,而且添加曲线也非常好用。



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭