Как нарисовать труегольник произвольного размера и цвета в pyqt5

При нажатии на пробел, на месте курсора должен рисоваться треугольник(равносторонний, с произвольными сторонами и цветом заливки).

from random import randint
import sys

from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt

RECT = 1
TRIANGLE = 2

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

        self.minSize = 300
        self.maxSize = 1000
        self.w = 0
        self.h = 0
        self.getRandomWinSize()
        self.what_to_draw = None

        self.initUI()

    def getRandomWinSize(self):
        self.w = randint(self.minSize, self.maxSize)
        self.h = randint(self.minSize, self.maxSize)

    def getRandomSize(self):
        return randint(10, self.minSize)

    def getRandomColor(self):
        return QColor(randint(0, 255), randint(0, 255), randint(0, 255))

    def initUI(self):
        self.resize(self.w, self.h)
        self.mouseCoord = None
        self.setMouseTracking(True)

    def draw_rect(self, qp):
        a = self.getRandomSize()
        qp.setBrush(self.getRandomColor())
        qp.drawRect(*self.mouseCoord, a, a)

    def draw_triangle(self, qp):
        pass
    def paintEvent(self, event):
        qp = QPainter()
        qp.begin(self)

        if self.what_to_draw == RECT:
            self.draw_rect(qp)
        elif self.what_to_draw == TRIANGLE:
            pass

        qp.end()

    def mouseMoveEvent(self, event):
        self.mouseCoord = event.x(), event.y()

    def mousePressEvent(self, event):
        if event.button() == Qt.RightButton:
            self.what_to_draw = RECT
            self.repaint()

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Space:
            self.what_to_draw = TRIANGLE
            self.repaint()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec())

Ответы (1 шт):

Автор решения: S. Nick

Попробуйте так:

import sys
import math                                                           # +++
from random import randint
from PyQt5.QtGui import QPainter, QColor, QPainterPath
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt, QPointF
from PyQt5 import QtCore, QtGui, QtWidgets


RECT = 1
TRIANGLE = 2


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

        self.minSize = 300
        self.maxSize = 1000
        self.w = 0
        self.h = 0
        self.getRandomWinSize()
        self.what_to_draw = None

        self.initUI()

    def getRandomWinSize(self):
        self.w = randint(self.minSize, self.maxSize)
        self.h = randint(self.minSize, self.maxSize)

    def getRandomSize(self):
        return randint(10, self.minSize)

    def getRandomColor(self):
        return QColor(randint(0, 255), randint(0, 255), randint(0, 255))

    def initUI(self):
        self.resize(self.w, self.h)
        self.mouseCoord = None
        self.setMouseTracking(True)

    def draw_rect(self, qp):
        a = self.getRandomSize()
        qp.setBrush(self.getRandomColor())
        qp.drawRect(*self.mouseCoord, a, a)
        
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    def draw_triangle(self, qp):
        a = self.getRandomSize()
        d = a * math.tan(math.radians(30))
        x, y = self.mouseCoord

        pos_top = QPointF(*self.mouseCoord)
        pos_left = QPointF(x - d, y + a)
        pos_right = QPointF(x + d, y + a) 
        
        qp.setBrush(self.getRandomColor())
        path = QPainterPath()
        path.moveTo(pos_top)
        path.lineTo(pos_right)
        path.lineTo(pos_left)

        qp.drawPath(path) 
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        
        
    def paintEvent(self, event):
        qp = QPainter()
        qp.begin(self)

        if self.what_to_draw == RECT:
            self.draw_rect(qp)
        elif self.what_to_draw == TRIANGLE:
            self.draw_triangle(qp)                                               # +++
#            pass
        qp.end()

    def mouseMoveEvent(self, event):
        self.mouseCoord = event.x(), event.y()

    def mousePressEvent(self, event):
        if event.button() == Qt.RightButton:
            self.what_to_draw = RECT
            self.repaint()

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Space:
            self.what_to_draw = TRIANGLE
            self.repaint()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec())

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

→ Ссылка