QCustomPlot 解决新版本中没有removeDataBefore()

QCustomPlot的v2.x版本移除了removeDataBefore()函数,但在动态图绘制中常用到。本文介绍如何通过修改QCustomPlot源码,在QCPGraph类中添加此功能,以避免内存增加。详细步骤包括修改qcustomplot.h和qcustomplot.cpp文件,以及展示如何设置不同坐标轴显示两条曲线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

QCustomPlot V1.x版本中是有removeDataBefore()的,可以删除之前的数据,但是更新到v2.x时,这个函数被取缔了,但它经常会在绘制动态图中用到。

当点的数据超出显示范围之后,最好使用removeDataBefore()删除范围外的数据,不然内存将一直增加,QCustomPlot不会自己删除。


这里我们修改QCustomPlot源码进行添加,在QCPGraph类中增加公有成员函数。

1、修改qcustomplot.h,在QCPGraph中增加一行

void removeDataBefore(int size);  //  增加的 V2.X

2、修改qcustomplot.cpp

 mDataContainer->removeBefore(mDataContainer->size()-size);

3、显示两条曲线 并设置显示不同的坐标轴

 

如果您的版本没有`QCPItemPolygon`,您可以使用`QCPItemLine`和`QCPItemCurve`来绘制下三角形。以下是代码示例: ```cpp // 获取频谱图最大值的索引 int indexMax = ui->customPlot->graph(0)->data()->indexOfMaxValue(); // 获取频谱图最大值的坐标 QCPData maxData = ui->customPlot->graph(0)->data()->at(indexMax); double xMax = maxData.key; double yMax = maxData.value; // 创建下三角形图标 QCPItemLine *line1 = new QCPItemLine(ui->customPlot); line1->start->setCoords(xMax-10, yMax-10); line1->end->setCoords(xMax+10, yMax-10); line1->setPen(QPen(Qt::red, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); QCPItemLine *line2 = new QCPItemLine(ui->customPlot); line2->start->setCoords(xMax-10, yMax-10); line2->end->setCoords(xMax, yMax); line2->setPen(QPen(Qt::red, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); QCPItemLine *line3 = new QCPItemLine(ui->customPlot); line3->start->setCoords(xMax, yMax); line3->end->setCoords(xMax+10, yMax-10); line3->setPen(QPen(Qt::red, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); // 更新图形 ui->customPlot->replot(); ``` 在上述代码中,我们首先通过`indexOfMaxValue()`函数获取频谱图最大值的索引,然后通过`at()`函数获取该点的坐标。接着,我们创建了三条线段,分别表示三角形的三条边,并设置其颜色和宽度。最后,我们通过`setCoords()`函数设置图标的位置和大小,然后调用`replot()`函数更新图形。 这里我们使用了`QCPItemLine`和`QCPItemCurve`来绘制下三角形,通过设置线段的起点和终点坐标,来绘制三角形的三边。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值