'How can I add new ellipse while retaining the previous ellipse?

Currently, my code can paint an ellipse but every time I click the mouse for a new ellipse the previous ellipse will not retain .

I am used the **QGraphicsView** Framework because I am using its pantool and zoom function but I have a hard time adding the paint function.

import sys
from PyQt5.QtCore import Qt, QRectF, QPointF
from PyQt5.QtGui import QPixmap, QTransform, QBrush, QColor, QPen, QPainterPath
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QGraphicsView, 
QGraphicsScene, QGraphicsPixmapItem, QSizePolicy, QSpacerItem, QGraphicsObject

class MouseBrushObject(QGraphicsObject):
    def __init__(self):
        QGraphicsObject.__init__(self)
        self._size = 10
        self._x = 0
        self._y = 0
        self._pen = None
        self._brush = None
        self._color = None
        self.setColor(QColor(255, 0, 0, 255))
 
    def paint(self, painter, option, widget):
        rect = self.boundingRect()
        painter.setPen(self._pen)
        painter.setBrush(self._brush)
        painter.drawEllipse(QPointF(self._x,self._y), 5,5)
 
    def boundingRect(self):
        return QRectF(self._x, self._y, self._size, self._size)
 
    def setColor(self, color):
        self._color = color
        self._pen = QPen(self._color, 1)
        self._brush = QBrush(QColor(self._color.red(), self._color.green(), self._color.blue(), 40))
 
    def setSize(self, size):
        self._size = size
        
    def setPosition(self, pos):
        self._x = pos.x() - pos.x()/2
        self._y = pos.y() - pos.y()/2
        self.setPos(QPointF(self._x, self._y))

 
 
class View(QGraphicsView):
    def __init__(self, parent=None):
        QGraphicsView.__init__(self, parent=parent)
        self.setMouseTracking(True)
        self.scene = QGraphicsScene(self)
        self.setScene(self.scene)
        pixmap = QPixmap(300, 300)
        self.scene.addItem(QGraphicsPixmapItem(pixmap))
        #self.setTransform(QTransform().scale(1, 1).rotate(0))
        self.scene.setBackgroundBrush(QBrush(Qt.lightGray))
        self._brushItem = MouseBrushObject()
        self.path = QPainterPath()
 
    def mousePressEvent(self, event):
        pos = event.pos()
        self._brushItem.setPosition(pos)
    
        
 
    def enterEvent(self, event):
        self.scene.addItem(self._brushItem)
        return super(View, self).enterEvent(event)
 
    def leaveEvent(self, event):
        self.scene.removeItem(self._brushItem)
        return super(View, self).leaveEvent(event)
 
 
class Viewer(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent=parent)
 
        layout = QVBoxLayout()
        self.view = View(self)
        self.setLayout(layout)
 
        layout.addWidget(self.view)
 
 
class MainWindow(QMainWindow):
 
    def __init__(self):
        QMainWindow.__init__(self)
        self.viewer = Viewer(self)
 
        layout = QVBoxLayout()
        layout.addWidget(self.viewer)
        centralwidget = QWidget(self)
        centralwidget.setLayout(layout)
        self.setCentralWidget(centralwidget)
 
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

Every time the mousePressEvent is trigger, it will paint an ellipse but the problem is that it will not retain the previous ellipse will adding the new ellipse.

My goal is for this code is two paint an ellipse when the mouse is clicked as it will serve as a point. Then The two points will be automatically connect using path. I have tried it will the qpainter function and it works but since I can't used qpaintEvent in QgraphicsView, I have a hard time executing the code.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source