Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QPainter::drawText consumes a lot of memory

    General and Desktop
    2
    8
    4079
    Loading More Posts
    • 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
      walteste last edited by

      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 Reply Quote 0
      • L
        lgeyer last edited by

        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 Reply Quote 0
        • W
          walteste last edited by

          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 Reply Quote 0
          • W
            walteste last edited by

            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 Reply Quote 0
            • L
              lgeyer last edited by

              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 Reply Quote 0
              • W
                walteste last edited by

                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 Reply Quote 0
                • L
                  lgeyer last edited by

                  Fine. ;-)

                  1 Reply Last reply Reply Quote 0
                  • W
                    walteste last edited by

                    Thanks for your input.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post