Important: Please read the Qt Code of Conduct -

How to undraw a QPainterPath?

  • I can see from the Qt docs and examples on how to use QPainter::drawPath(), but how do I "undraw" a path when I want to remove it from the display area of the widget without repainting the entire widget?

  • what you want is impossible - the painting event is just doing that painting everything again. you have 2 options

    1. most simple - paint everything again without that portion of the path
    2. paint the background colour on the same path (blend the path with the background together - like that you have the impression that has been deleted)
    3. this is more like 1 but depending on your application, so basically imagine that you are doing a 3d or 2d cad app where you have the walls of the room - if the user moves one wall what you are doing is just deleting the last object and just paint a new one with the coordinates from starting point to the mouse coordinates

  • Is there a way to paint only a small region of the screen to improve painting speed? It has been long time since I have worked with painting, so I remember vaguely with win32 that you could mark a smaller region for painting and then of course in the code you re-paint only that region needing an update. Is there something similar in Qt? Also, I'm coming from a WPF/UWP background and in that framework I would basically create a "UserControl" (QWidget) that just draw the arrow I need onto a Canvas allowing free positioning, no layout enforcement and the UserControl would be responsible to repaint itself, and if I want to remove it from the "Canvas" it is simple as removing it from a collection of "Children". I hope that makes sense to you. I'm struggling to do the same in Qt.

    so basically I envision that I should be able to specify a minimal rectangular region that contains the path element and then repaint only that area.

    Or perhaps I need to implement this differently? Do I just use a QWidget and is there sometime similar to WPF/UWP "Canvas" object in Qt that I should be using? As it is now, I have a custom QWidget which I use all the required drawing functions to display what I need to display during paintEvent.

  • @Snorkelbuckle said in How to undraw a QPainterPath?:

    Is there a way to paint only a small region of the screen to improve painting speed?

    Look at this QPainter methods:
    setClipRect(), setClipPath(), and setClipRegion().

    I need onto a Canvas allowing free positioning,

    Something like QGraphicsView and QGraphicScene ?

  • @mpergand, thanks for that!

    It looks like I need to use QGraphicsView and QGraphicsScene. These two come the closest to what I'm used to with UWP. In terms of UWP, with these I can add a QPainterPath (or other shapes) to the QGraphicsScene's "children" collection and it is drawn automatically and then I can remove the QPainterPath simply with the removeItem() method and it is removed from the display once I delete the removed item.

    This is good, I can work with this.

    As a side note, whether or not it actually utilizes region clipping during repaint operations, I don't know (I suspect it does since it seems pretty snappy with the load I put on it during testing). But I don't really care, I can handle the overhead since I was really looking for a better way to handle the add and remove of custom shapes from the painting area.


Log in to reply