文章目录

问题

实时波形分析,波形显示刷新周期500ms,刷新宽度为310点;在显示宽度为800时,不时出现程序忙的圆圈状态,界面有些卡。

分析

显示处理最主要部分是往LineSeries中添加逐个添加节点。

//向单个LineSeries中添加节点
void CCurveView::addSeries(int iChan, const QList<QPointF> *pListPoint)
{
    assert(iChan < m_iChanCnt);
    QLineSeries *pSeries = &m_series[iChan];
    double *pMin = &m_pdYMin[iChan];
    double *pMax = &m_pdYMax[iChan];
    for (QList<QPointF>::const_iterator itr = pListPoint->constBegin(); itr != pListPoint->constEnd(); itr++)
    {
        QPointF pt(itr->x(), itr->y() + iChan * m_iChanSpan);
        if (pt.y() > *pMax) { *pMax = pt.y();}
        else if (pt.y() < *pMin) {*pMin = pt.y();}
        pSeries->append(pt);
    }
    int iCnt = pSeries->count();
    if (iCnt > m_iPtCapacity)
    {
        pSeries->removePoints(0, iCnt - m_iPtCapacity);
    }
}

由于程序中有多个LineSeries,往每个LineSeries中更新数据时,每次更新一个点;怀疑这里太慢;修改方案:1)每个LineSeries,一次更新一个List;2)添加函数,更新所有LineSeries;

void CCurveView::addSeries(const QList<QPointF> *pListPoint, int iChanNum)
{
    assert(iChanNum <= m_iChanCnt);
    for (int iChan = 0; iChan < iChanNum; iChan++)
    {
        QLineSeries *pSeries = &m_series[iChan];
        double *pMin = &m_pdYMin[iChan];
        double *pMax = &m_pdYMax[iChan];
        QList<QPointF> listPtTmp;
        int iCnt = pSeries->count() + pListPoint->count(), iSub;

        for (QList<QPointF>::const_iterator itr = pListPoint->constBegin(); itr != pListPoint->constEnd(); itr++)
        {
            QPointF pt(itr->x(), itr->y() + iChan * m_iChanSpan);
            if (pt.y() > *pMax) { *pMax = pt.y();}
            else if (pt.y() < *pMin) {*pMin = pt.y();}
            listPtTmp.append(pt);
        }
        if (iCnt > m_iPtCapacity)
        {
            iSub = iCnt - m_iPtCapacity;
            assert(iSub <= pSeries->count());
            pSeries->removePoints(0, iSub);
            iCnt = m_iPtCapacity;
        }
        pSeries->append(listPtTmp);
        pListPoint++;
    }
}

但效果不明显。
在网上查找解决方法,有人提出,更新数据时将LineSeries移除,数据整理完后,再添加。

void CCurveView::addSeries(const QList<QPointF> *pListPoint, int iChanNum)
{
    assert(iChanNum <= m_iChanCnt);
    for (int iChan = 0; iChan < iChanNum; iChan++)
    {
        QLineSeries *pSeries = &m_series[iChan];
        double *pMin = &m_pdYMin[iChan];
        double *pMax = &m_pdYMax[iChan];
        QList<QPointF> listPtTmp;
        int iCnt = pSeries->count() + pListPoint->count(), iSub;
        m_chart->removeSeries(pSeries);//remove line series
        for (QList<QPointF>::const_iterator itr = pListPoint->constBegin(); itr != pListPoint->constEnd(); itr++)
        {
            QPointF pt(itr->x(), itr->y() + iChan * m_iChanSpan);
            if (pt.y() > *pMax) { *pMax = pt.y();}
            else if (pt.y() < *pMin) {*pMin = pt.y();}
            listPtTmp.append(pt);
        }
        if (iCnt > m_iPtCapacity)
        {
            iSub = iCnt - m_iPtCapacity;
            assert(iSub <= pSeries->count());
            pSeries->removePoints(0, iSub);
            iCnt = m_iPtCapacity;
        }
        pSeries->append(listPtTmp);

        m_chart->addSeries(pSeries);//add line series
        m_chart->setAxisX(m_axisX, pSeries);
        m_chart->setAxisY(m_axisY, pSeries);
        pListPoint++;
    }
}

结果发现:明显刷新速度变快。
于是,我将显示宽度调整为8000,刷新宽度调整为3510,也没有发现明显的滞后。

总结

优化模式:先移除LineSeries,添加数据,再添加。

引用

https://www.qtcentre.org/threads/69429-SOLVED-QLineSeries-extremely-slow


版权声明:本文为ldhshao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ldhshao/article/details/111603378