D
D
DanielKkk2018-04-28 19:51:44
Python
DanielKkk, 2018-04-28 19:51:44

How to fix error when drawing QPixmap image?

1) When designing the background through QPixMap, an error is displayed, and if you use self.image to set the background, the picture is loaded easily and you can draw
2) How to fix my code so that it works optimally using QPixmap

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QHBoxLayout
from PyQt5.QtGui import QPainter, QColor, QMouseEvent, QImage, QPixmap, QPen
from PyQt5.QtCore import Qt

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.flag = False
        self.initUI()
    def initUI(self):
        hbox = QHBoxLayout(self)
        pixmap = QPixmap('picture.png')
        painter = QPainter(pixmap)
        painter.drawEllipse(29, 29, 29, 29)
        painter.end()
        label = QLabel()
        label.setPixmap(pixmap)
        hbox.addWidget(label)
        self.show()

    def mousePressEvent(self, e):
        if e.button() == Qt.LeftButton:
            self.flag = True
            self.paint = QPainter(self.image)
            self.ellips(e)

    def paintEvent(self, e):
        paint = QPainter(self)
        paint.drawImage(0, 0, self.image)

    def mouseMoveEvent(self, e):
        if self.flag:
            print(e.pos())
            self.ellips(e)

    def ellips(self, e):
        self.paint.setBrush(QColor('yellow'))
        self.paint.drawEllipse(e.pos(), 20, 20)

        self.update()
app = QApplication(sys.argv)
w = Example()
sys.exit(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