使用QCustomPlot 第三方库,replot()性能优化

项目的需求:因为项目要求2ms每次从硬件中获取4000数据进行实时显示,数据量大、刷新速度快。

软件存在的问题:大概软件运行3-5分钟就一直报:QBackingStore::endPaint() called with active painter; did you forget to destroy it or call QPainter::end() on it?这个警告,过一会软件就闪退了,查找原因发现是:ui->customPlot->replot()的原因。

于是就百度replot()函数的性能优化,结果发现有网友说ui->customPlot->replot(QCustomPlot::rpQueuedReplot)可以优化性能,复制粘贴后发现代码报错,没有找到这个函数,于是就迷茫了,我的咋不行呀。。咋办。。咋办。

转到replot()源码,发现和网上别人的源码截图核对下,不一样,为什么不一样呢??首先考虑是不是QCustomPlot版本的问题,于是下载和最新版本查看下replot()函数源码,这次和网上对的上了,如果你的这个函数源码不是如下:

void QCustomPlot::replot(QCustomPlot::RefreshPriority refreshPriority)
{
  if (refreshPriority == QCustomPlot::rpQueuedReplot)
  {
    if (!mReplotQueued)
    {
      mReplotQueued = true;
      QTimer::singleShot(0, this, SLOT(replot()));
    }
    return;
  }
  
  if (mReplotting) // incase signals loop back to replot slot
    return;
  mReplotting = true;
  mReplotQueued = false;
  emit beforeReplot();
  
# if QT_VERSION < QT_VERSION_CHECK(4, 8, 0)
  QTime replotTimer;
  replotTimer.start();
# else
  QElapsedTimer replotTimer;
  replotTimer.start();
# endif
  
  updateLayout();
  // draw all layered objects (grid, axes, plottables, items, legend,...) into their buffers:
  setupPaintBuffers();
  foreach (QCPLayer *layer, mLayers)
    layer->drawToPaintBuffer();
  foreach (QSharedPointer<QCPAbstractPaintBuffer> buffer, mPaintBuffers)
    buffer->setInvalidated(false);
  
  if ((refreshPriority == rpRefreshHint && mPlottingHints.testFlag(QCP::phImmediateRefresh)) || refreshPriority==rpImmediateRefresh)
    repaint();
  else
    update();
  
# if QT_VERSION < QT_VERSION_CHECK(4, 8, 0)
  mReplotTime = replotTimer.elapsed();
# else
  mReplotTime = replotTimer.nsecsElapsed()*1e-6;
# endif
  if (!qFuzzyIsNull(mReplotTimeAverage))
    mReplotTimeAverage = mReplotTimeAverage*0.9 + mReplotTime*0.1; // exponential moving average with a time constant of 10 last replots
  else
    mReplotTimeAverage = mReplotTime; // no previous replots to average with, so initialize with replot time
  
  emit afterReplot();
  mReplotting = false;
}

说明你的版本不是最新的!!!请到:https://www.qcustomplot.com/index.php/download,下载最新的包。

void connectCamera::doReceiveData_slots(float* m_pLineZBuf)
{
    ui->customPlot->graph(0)->data().data()->clear();
    double xdata=0.00;
    for (int i=0;i<3995;i++)
    {
        xdata+=0.01;//4000点对应40mm
        ui->customPlot->graph(0)->addData(xdata,*(m_pLineZBuf+i));
    }
    ui->customPlot->replot(QCustomPlot::rpQueuedReplot);//就是这行代码出现的问题
}

最后要感谢:https://blog.csdn.net/weixin_41712355/article/details/82689382  这篇博主给我的提示,让我解决了上述问题,非常感谢!!

  • 2
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值