'How can I connect two created ellipses?

I have a code that will paint an ellipse whenever the mouse is clicked.I want to connect the previous ellipse with the new painted ellipse with a line like in the picture here

My code is

import sys
from turtle import pos
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 = QPen(Qt.red, 5)
        self._brush = QBrush(Qt.red, 5)
        self._px = 0
        self._py = 0
 
    def paint(self, painter, option, widget):
        #rect = self.boundingRect()
        painter.setPen(self._pen)
        painter.setBrush(self._brush)
        painter.drawEllipse(QPointF(self._x,self._y), 10,10)
        self._px = self._x
        self._py = self._y
        print(QPointF(self._x,self._y))
        print(self._x)
        print(self._px)
        
        #painter.drawLine(self._x,self._y, 5, 5)
 
    def boundingRect(self):
        return QRectF(self._x, self._y, self._size, self._size)
 
    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._scene.setBackgroundBrush(QBrush(Qt.lightGray))
        self.objects = []
 
    def mousePressEvent(self, event):
        pos = event.pos()
        obj = MouseBrushObject()
        obj.setPosition(pos)
        self.objects.append(obj)
        self._scene.addItem(obj)
 
 
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_())

I am thinking of adding drawLine in the paint function, but I can't retrieve the previous x and y because I don't know how to save them. Also, I am thinking of using the qpainterpath, but I don't know how to code it.



Solution 1:[1]

(Update)I have already found a way to drawLine using the previous and present ellipse. The points are correct since I have printed it:

enter image description here

but the problem is will not perfectly fit the two ellipses like in this picture:

result.

I know that the points from the previous and present points are correct, but the line is not corretly drawn.

import sys
from turtle import pos
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 = QPen(Qt.red, 5)
        self._brush = QBrush(Qt.red, 5)

 
    def paint(self, painter, option, widget):
        #rect = self.boundingRect()
        painter.setPen(self._pen)
        painter.setBrush(self._brush)
        painter.drawEllipse(QPointF(self._x,self._y), 10,10)
        #print("hello")
        #print(QPointF(self._x,self._y))
        #print(self._x)
        #print(self._px)
        if not self.prevpos:
            print("current xy: "+"("+str(self._x)+","+str(self._y)+")")
        else:
            print("previous xy: "+"("+str(self._px)+","+str(self._py)+")")
            print("new current xy: "+"("+str(self._x)+","+str(self._y)+")")
            
            painter.drawLines(QPointF(self._px,self._py),QPointF(self._x,self._y))
            #painter.drawLine(self._px,self._py,self._x,self._y)
 
    def boundingRect(self):
        return QRectF(self._x, self._y, self._size, self._size)
 
    def setSize(self, size):
        self._size = size
        
    def setPosition(self, pos,prevpos):
        self.prevpos = prevpos
        #print("meow")
        #print(prevpos)
        if not prevpos:
            self._x = pos.x() - pos.x()/2
            self._y = pos.y() - pos.y()/2
            #self.setPos(QPointF(self._x, self._y))
        else:
            self._x = pos.x() - pos.x()/2
            self._y = pos.y() - pos.y()/2
            self._px = prevpos.x() - prevpos.x()/2
            self._py = prevpos.y() - prevpos.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.objects = []
        self.posprev = ""
 
    def mousePressEvent(self, event):
        
        pos = event.pos()
        
        if not self.posprev:
            obj = MouseBrushObject()
            obj.setPosition(pos,self.posprev)
            self.objects.append(obj)
            self._scene.addItem(obj)
            self.posprev = pos
            #print(pos)
        
        else:
            #print(self.posprev)
            #print(pos)
            obj = MouseBrushObject()
            obj.setPosition(pos,self.posprev)
            self.objects.append(obj)
            self._scene.addItem(obj)
            self.posprev =pos
            
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_())

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 musicamante