QPainter - must it be deleted after draw ?



  • 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


  • Moderators

    @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.



  • @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 ?


  • Moderators

    @ranshalit

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

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.