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 - must it be deleted after draw ?
Forum Updated to NodeBB v4.3 + New Features

QPainter - must it be deleted after draw ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.4k Views 2 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.
  • R Offline
    R Offline
    ranshalit
    wrote on last edited by ranshalit
    #1

    Hello,

    I am trying to understand how to properly use QPainter.
    I need to use qpainter to paint more than one polygon on window.
    I thought of using it with QPixmap for this issue.
    Yet, I saw that the previous painting was not cleared untill I used
    pixmap.fill()

    I don't have the exact code, but it goes something like this:

    *constructor:
    ...
     
    m_pix = new QPixmap(480,480);
    m_painter = new QPainter(m_pix);
    ...
    m_label->addPixmap(*m_pix);
    ....
    }
    
    func_draw_polygon()
    {
    ...
    m_painter.setBrush( Qt::red );
    m_painter.drawPolygon(m_poly);
    ...
    }
    

    The thing is that on calling func_draw_polygon() multiple times with different polygon, I see that the previous draw is still there (unless using fill in func_draw_polygon() - which works fine).
    I than saw in documentation the following:
    "Remember to destroy the QPainter object after drawing"

    What does it mean ? Can QPainter be used as a private member as I did ?
    Is it advised in documentation to be deleted, so that a new painting will not re-paint the previous paintings again ?

    Thanks,
    Ran

    raven-worxR 1 Reply Last reply
    0
    • R ranshalit

      Hello,

      I am trying to understand how to properly use QPainter.
      I need to use qpainter to paint more than one polygon on window.
      I thought of using it with QPixmap for this issue.
      Yet, I saw that the previous painting was not cleared untill I used
      pixmap.fill()

      I don't have the exact code, but it goes something like this:

      *constructor:
      ...
       
      m_pix = new QPixmap(480,480);
      m_painter = new QPainter(m_pix);
      ...
      m_label->addPixmap(*m_pix);
      ....
      }
      
      func_draw_polygon()
      {
      ...
      m_painter.setBrush( Qt::red );
      m_painter.drawPolygon(m_poly);
      ...
      }
      

      The thing is that on calling func_draw_polygon() multiple times with different polygon, I see that the previous draw is still there (unless using fill in func_draw_polygon() - which works fine).
      I than saw in documentation the following:
      "Remember to destroy the QPainter object after drawing"

      What does it mean ? Can QPainter be used as a private member as I did ?
      Is it advised in documentation to be deleted, so that a new painting will not re-paint the previous paintings again ?

      Thanks,
      Ran

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @ranshalit
      it's ok to create a QPainter on the stack (means it will be deleted automatically when it goes out of scope).
      If you want to use it like you do, you need to call begin() and end() on the painter.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      R 1 Reply Last reply
      2
      • raven-worxR raven-worx

        @ranshalit
        it's ok to create a QPainter on the stack (means it will be deleted automatically when it goes out of scope).
        If you want to use it like you do, you need to call begin() and end() on the painter.

        R Offline
        R Offline
        ranshalit
        wrote on last edited by ranshalit
        #3

        @raven-worx said in QPainter - must it be deleted after draw ?:

        begin

        Hi,
        It works without begin/end in my implementation.
        Does begin clears previous painting ?

        raven-worxR 1 Reply Last reply
        0
        • R ranshalit

          @raven-worx said in QPainter - must it be deleted after draw ?:

          begin

          Hi,
          It works without begin/end in my implementation.
          Does begin clears previous painting ?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @ranshalit

          • being() -> before you start painting
          • end() -> when you're done painting

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1

          • Login

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