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. paintEvent override
Qt 6.11 is out! See what's new in the release blog

paintEvent override

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 7.4k Views 3 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.
  • AllForTheGameA Offline
    AllForTheGameA Offline
    AllForTheGame
    wrote on last edited by
    #1

    Hi everyone...
    I'm working on a qt project where I need to redefine the paintEvent function...
    I'm using this fonction with a qpainter and this way:

    QPainter painter(this);
    

    But, everytime my window get scaled, this widget create a new qpainter and increase the memory size of my program... (140k to 300k)
    Is there any way to delete the older QPainter to free the memory???

    Thx

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Normally you have like this

      widget::PaintEvent(xx) {
      QPainter painter(this);
      }

      and painter only lives until paintEvent ends

      so maybe its something else that happens when you scale the window?

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

        Hi and welcome to devnet,

        Can you show the implementation of your paint event ?

        Currently, your painter object will be deleted at the end of the function.

        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
        • AllForTheGameA Offline
          AllForTheGameA Offline
          AllForTheGame
          wrote on last edited by AllForTheGame
          #4

          Here is my function:

          void Histogram::paintEvent(QPaintEvent *event)
          {
          Q_UNUSED(event);

          QPainter painter(this);
          int i = 0;
          
          if(data.count() > 0)
          {
              double barWidth = (width() * 0.9) / (double)data.size();
              uint max = 0;
              double step = 0, height = this->height() * 0.75;
              QLineF horAxe(width() * 0.05, this->height()*0.9, width() * 0.95, this->height()*0.9);
              QLineF verAxe(width() * 0.05, this->height()*0.9, width() * 0.05, this->height()*0.1);
          
              for(i=0; i < data.size(); i++)
              {
                  if (data[i] > max)
                      max = data[i];
              }
          
              painter.drawLine(horAxe);
              painter.drawLine(verAxe);
              painter.drawText(width() * 0.04, this->height() * 0.95, "0");
              painter.drawText(width() * 0.96, this->height() * 0.95, QString::number(data.size() - 1));
          
              step = height / max;
          
              for(i=0; i < data.size(); i++)
              {
                  double start = this->height()*(0.75 + 0.25 /2) - (data[i] * step);
                  double startWidth = (barWidth * i) + barWidth * 0.05 + width() * 0.05;
          
                  painter.fillRect(startWidth, start, barWidth * 0.9, data[i] * step, Qt::green);
                  painter.drawRect(startWidth, start, barWidth * 0.9, data[i] * step);
              }
          }
          

          }

          Normally the object should be destroyed but I don't know cause it gets the widget as a parent, so coud it be possible it never get deleted???

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

            Does the memory foot print change each time you make your window bigger ? Or both making it it smaller and bigger triggers that ?

            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
            • AllForTheGameA Offline
              AllForTheGameA Offline
              AllForTheGame
              wrote on last edited by
              #6

              Both triggers that

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

                Can you run your application through Valgrind to see what it happening with the memory ?

                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

                • Login

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