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. QPainter::drawText consumes a lot of memory
QtWS25 Last Chance

QPainter::drawText consumes a lot of memory

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 4.5k Views
  • 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.
  • W Offline
    W Offline
    walteste
    wrote on last edited by
    #1

    Hi,

    with this code, my program consumes an additional 100MB of Memory on a Windows 7 x64 - MSVC10 computer using Qt 4.7.4, 4.8.1 and 4.8.2.

    Is there any way to reduce or disable the caching that happens?

    @void MyWidget::paintEvent(QPaintEvent *event)
    {
    QPainter *p = new QPainter(this);

    for (int s=5; s<500; s++)
    {
        QFont *f = new QFont(p->font());
        f->setPixelSize(s);
        p->setFont(*f);
        delete f;
        p->drawText(0,this->height(),"Hello World");
    }
    
    delete p;
    

    }@

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      I don't think caching is the problem; you memory allocation strategy and your code is.

      @
      void MyWidget::paintEvent(QPaintEvent *event)
      {
      QPainter p(this);
      QFont f(p.font());

      int fHeight = height();
       
      for (int s=5; s<500; s++)
      {
          f.setPixelSize(s);
          p.drawText(0, fHeight, "Hello World");
      }
      

      }
      @

      1 Reply Last reply
      0
      • W Offline
        W Offline
        walteste
        wrote on last edited by
        #3

        No, i am deleting all my allocated objects. I have tried your code and that has the problem that the font is not getting increased for the painter. I have corrected your code and then the memory usage is back as described above already.

        @void MyWidget::paintEvent(QPaintEvent *event)
        {
        QPainter p(this);
        QFont f(p.font());

        int fHeight = height();
        
        for (int s=5; s<500; s++)
        {
            f.setPixelSize(s);
            p.setFont(f);
            p.drawText(0, fHeight, "Hello World");
        }
        

        }@

        [quote author="Lukas Geyer" date="1341817685"]I don't think caching is the problem; you memory allocation strategy and your code is.[/quote]

        1 Reply Last reply
        0
        • W Offline
          W Offline
          walteste
          wrote on last edited by
          #4

          I just found out that Qt does some sort of garbage collection from time to time in the font cache. This is good and explains that there is no memory leakage.

          But is there a way to limit this cache in size?

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            I don't think there is a public API to manipulate the font cache, but probably there should be one. Feel free to file a "bug report":http://qt-project.org/doc/qt-5.0/bughowto.html.

            Is this a real world example or just some theortical code?

            1 Reply Last reply
            0
            • W Offline
              W Offline
              walteste
              wrote on last edited by
              #6

              I have created a "bug report":https://bugreports.qt-project.org/browse/QTBUG-26463 and got already a constructive answer which solves the issue.

              @void MyWidget::paintEvent(QPaintEvent *event)
              {
              QPainter p(this);
              QFont f(p.font());

              int fHeight = height();

              for (int s=5; s<500; s++)
              {
              f.setPixelSize(s);
              p.setFont(f);
              p.drawText(0, fHeight, "Hello World");
              }
              QFont::cleanup();
              }
              @

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                Fine. ;-)

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  walteste
                  wrote on last edited by
                  #8

                  Thanks for your input.

                  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