0

I am creating a desktop application using PyQt5 where the user will be able to draw rectangles on QGraphicsScene.

User should be able to select the top-left corner of the rectangle with first mouse click and the bottom-right corner with second mouse click. A rectangle should appear in that location with the perimeter well defined. I created application but have a problem I am not able to draw rectangles on scene.

Please find the below code for reference

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtGui, QtCore
from PyQt5.QtGui import QPainter, QPen, QBrush
from PyQt5.QtCore import Qt

class Windo(QWidget):
    def __init__(self):
        super().__init__()

        self.setGeometry(0,0,500,500)
        self.setWindowTitle("pu")
        self.setWindowIcon(QtGui.QIcon('x.jpeg'))

        self.begin = QtCore.QPoint()
        self.end = QtCore.QPoint()
        self.scene = QGraphicsScene()
        view = QGraphicsView(self.scene, self)
        view.setGeometry(0,0,400,400)

        self.show()

    def paintEvent(self,event):
        qp = QPainter(self)
        qp.begin(self)
        qp.setPen(QPen(Qt.black, 6, Qt.SolidLine))
        qp.drawRect(QtCore.QRect(self.begin, self.end))
        qp.end()

    def mousePressEvent(self, event):
        self.begin = event.pos()
        self.end = event.pos()

    def mouseMoveEvent(self, event):
        self.end = event.pos()
        self.update()

    def mouseReleaseEvent(self, event):
        self.begin = event.pos()
        self.end = event.pos()

app = QApplication(sys.argv)
win = Windo()
sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Didn't you ask the same question yesterday? – Heike Feb 05 '20 at 11:42
  • see https://stackoverflow.com/questions/52728462/pyqt-add-rectangle-in-qgraphicsscene – S. Nick Feb 05 '20 at 13:50
  • Please avoid pointing me in your questions, it is not necessary and that you place it does not commit me or notify me to answer your questions, for me it is only noise apart from being annoying. – eyllanesc Feb 05 '20 at 14:01

0 Answers0