Skip to content Skip to sidebar Skip to footer

Clear `qgraphicsview` Immediately In Qt <= 5.12

The given code below is derived from another question on SO. It displays a QMainWindow with 4 QGraphicsView to draw with the mouse in it and a QPushButton to clear the 4 QGraphicsV

Solution 1:

Even using the clear() method does not imply that the view is cleaned but you have to set the path in the item. Going to the problem, an equivalent method is to establish a new QPainterPath, in addition it is not necessary to call the update() method of the scene. The use of clear() or set an empty QPainterPath are equivalent from the python side, but from the C++ side it causes the same memory that optimizes the application to be reused.

defclear_view(self):
    self.path = QPainterPath()
    self.item.setPath(self.path)

If you want to have compatibility for both versions you can use try-except:

defclear_view(self):
    try:
        self.path.clear()
    except AttributeError as e:
        self.path = QPainterPath()
    self.item.setPath(self.path)

Post a Comment for "Clear `qgraphicsview` Immediately In Qt <= 5.12"