X
X
xRA1Nx2022-02-12 10:55:35
PyQt
xRA1Nx, 2022-02-12 10:55:35

Drawing in PyQt5. How to set the area in the template to place QPainter?

I would like the drawing to take place in my template, in the specified area, namely within the gridLayout. How can this be implemented? The code below opens 2 forms, I understand why this happens, but have not yet figured out how to fix it.

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QWidget, QApplication, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap  # для машинки
from PyQt5.QtGui import QPainter  # для геометрических фигур

Form, Window = uic.loadUiType("test.ui")

app = QApplication([])
window = Window()
form = Form()
form.setupUi(window)
window.show()

layout = form.gridLayout

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.qp = QPainter()
        self.initUI()

    def initUI(self):
        self.setGeometry(200, 100, 400, 400)
        self.setWindowTitle('Рисование')
        self.pixmap = QPixmap("car2.png")
        self.lbl = QLabel(self)
        self.lbl.setPixmap(self.pixmap)
        self.show()

    def paintEvent(self, event):
        self.qp.begin(self)
        coords = 
        self.qp.setPen(Qt.red)

        self.qp.drawLine(*coords[0], *coords[1])
        self.qp.drawLine(*coords[1], *coords[2])
        self.qp.drawLine(*coords[2], *coords[0])
        self.qp.drawEllipse(*[250, 250], 20, 20)
        self.qp.drawRect(*[170, 170], *[20, 20])
        self.qp.end()


ex = Example()
app.exec()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question