Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QPainter and dynamic list of items
Forum Updated to NodeBB v4.3 + New Features

[Solved] QPainter and dynamic list of items

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    th.thielemann
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      th.thielemann
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Why not just use QVector<QLine> ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • T Offline
          T Offline
          th.thielemann
          wrote on last edited by
          #4

          Because this made a copy of every QLine, doesn't it?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            And why is that a problem to copy two ints or two doubles?

            Edit: and in the paint method, you can use references....

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • T Offline
              T Offline
              th.thielemann
              wrote on last edited by
              #6

              Just to avoid to do an unnecessary step.
              But I tried it out and performance is the same.
              Thanks!

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

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

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved