[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);

    update(); // trigger redraw


    and paint:
    @void Diagram::paint(QPainter painter)
    for (QLineF
    line : m_curve)
    // 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);

    Any comments?

  • Lifetime Qt Champion


    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.

  • Don't try to optimize if it is not 100% needed. Copying a QLine (just 4? ints) is not expensive.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.