0

Как создать эллипс или точку на месте клика левой клавишей мыши, которые соединяются линией?

Эллипс каждый раз рисуется новый, а старый остается.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt

class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.widthApp, self.heightApp = 700, 550

        self.initUI()

    def initUI(self):
        self.setGeometry(600, 300, self.width(), self.height())
        self.show()

    def paintEvent(self, e):
        self.paint = QPainter()
        self.paint.begin(self)
        self.paint.drawEllipse(3, 3)
        self.paint.end()

    def mousePressEvent(self, e):
        if e.buttons() == Qt.LeftButton:
            self.update()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    w = Example()
    sys.exit(app.exec_())

1 ответ 1

1
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt, QPoint

class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.widthApp, self.heightApp = 700, 550

        self.pos = QPoint(120, 70)                                # +++

        self.initUI()

    def initUI(self):
        self.setGeometry(600, 300, self.width(), self.height())
        self.show()

    def paintEvent(self, e):
        self.paint = QPainter()
        self.paint.begin(self)
        self.paint.drawEllipse(self.pos, 100, 50)                # +++
        self.paint.end()

    def mousePressEvent(self, e):
        if e.buttons() == Qt.LeftButton:
            self.pos = e.pos()                                   # +++
            self.update()


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Example()
    sys.exit(app.exec_())

введите сюда описание изображения


from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QPainter, QPainterPath, QPen, QColor
from PyQt5.QtCore import Qt, QPoint


class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.widthApp, self.heightApp = 700, 550

        self.pos = QPoint(120, 70)    
        self.posList = [QPoint(120, 70),]               # +

        self.initUI()

    def initUI(self):
        self.setGeometry(600, 150, self.width(), self.height())
        self.show()

    def paintEvent(self, e):
        self.paint = QPainter()
        self.paint.begin(self)

        self.paint.setPen(QPen(QColor("blue")))
        self.drawLine(self.paint) 
        self.paint.setPen(QPen(QColor("red")))

        self.paint.drawEllipse(self.pos, 10, 10) 
###
        for p in self.posList:                   # +
            self.paint.drawEllipse(p, 10, 10)

        self.paint.end()

    def mousePressEvent(self, e):
        if e.buttons() == Qt.LeftButton:
            self.pos = e.pos()                                   
            self.posList.append(self.pos)        # +
            self.update()

    def drawLine(self, qp):
        qp.drawLine(300, 10, 300, 200)

        path = QPainterPath()
        path.moveTo(100, 200)
        path.lineTo(300, 200)   
        qp.drawPath(path)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Example()
    sys.exit(app.exec_())

введите сюда описание изображения

1
  • В моем случае, каждый раз элипс должен рисоваться новый,а старый остается. @S.Nick
    – sheviv
    29 мая 2019 в 10:37

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.