Important: Please read the Qt Code of Conduct -

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

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

    m_pix = new QPixmap(480,480);
    m_painter = new QPainter(m_pix);
    m_painter.setBrush( Qt::red );

    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 ?


  • Moderators

    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 ?:


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

  • Moderators


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

Log in to reply