I'm trying to make a live plotting graph class in qt and the scrolling works. But when watching my program in Task Manager I recognized that the CPU and RAM usage increases by time (and data).
So I thought it would be a good style to use the remove function to delete data which is not shown anyway. Here's my code:
void Live_Chart::UpdateY(float yValue)
{
xValue++;
this->yValue = yValue;
m_series->append(xValue, yValue);
if (xValue > m_axisX->max())
{
// "Scroll" the data in the view
m_axisX->setMax(xValue);
m_axisX->setMin(m_axisX->min()+1);
// Remove the previous data we don't see
qDebug() << "Removing " << m_axisX->min();
m_series->remove(m_axisX->min());
}
}
This method is called every 100 ms with a random number in between 0 and 10.
But always when reaching removing=52 the program crashes with this error msg:
ASSERT failure in QVector<T>::remove: "index out of range", file c:\users\qt\work\qt\qtbase\include\qtcore\../../src/corelib/tools/qvector.h, line 483
And I really have no clue why 52. This number isn't specified in any part of my program. The range of the x-axis is 50. On the create of the QLineSeries I am adding the point (0, 0).
EDIT: When using a m_axisX range of (0, 20) the program crashes when trying to delete point 22.
--> The program crashes when deleting a point whith x = x-range + 2 (Tested with other numbers too).
You are confusing the index of the data points in the vector m_series
and their x
value. When the data point with x = old_x_min
goes out of the window you want to display, then that data point is at index 0
not at index old_x_min
.
Replace
m_series->remove(m_axisX->min());
with
m_series->remove(0);
to remove the oldest data point, instead of some data point in the middle of the series.