项目的需求:因为项目要求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 这篇博主给我的提示,让我解决了上述问题,非常感谢!!