[Solved] QPainter and dynamic list of items
-
I use a class inherited from QQuickPaintedItem to draw data curves in Qml.
The number of elements is unknown. And after 5s all data becomes invalid and a new data set starts.Therefore I have:
@QVector<QLineF*> m_curve;@and a slot to receive data:
@void Diagram::addDataPoint(int value1)
{
// Calculate line points
...
QLineF* m_curveElement = new QLine(startPoint, endPoint);
m_curve.append(m_curveElement)update(); // trigger redraw
}@
and paint:
@void Diagram::paint(QPainter painter)
{
for (QLineF line : m_curve)
{
painter->drawLine(*line);
}
// qDeleteAll(m_curve); --> Exception}@
- But this creates a memory leak, because there is no delete at all.
- But if I delete the lines in paint() I get an exception.
Question: When can I delete the created lines?
-
I found the following solution in my resetData method:
@void Diagram::resetData()
{
QVector<CMyLine*> m_tmpCurveFlow(m_curve);
m_curve.clear();
this->update();
qDeleteAll(m_tmpCurve);
}@Any comments?
-
Hi,
Why not just use QVector<QLine> ?
-
Because this made a copy of every QLine, doesn't it?
-
And why is that a problem to copy two ints or two doubles?
Edit: and in the paint method, you can use references....
-
Just to avoid to do an unnecessary step.
But I tried it out and performance is the same.
Thanks! -
Don't try to optimize if it is not 100% needed. Copying a QLine (just 4? ints) is not expensive.