Unsolved QLabel Image setScaledContents(True) don't allow Qpainter updates
-
Below code paints a point on Image for every click, however it works only if ("self.imglabel.setScaledContents(True)") is commented. Any reason?
import sys from PyQt5.QtCore import Qt, QPoint from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel, QSizePolicy, QMessageBox from PyQt5.QtGui import QPixmap, QPainter, QPen, QColor, QImage, QPalette class Menu(QMainWindow): def __init__(self): super().__init__() self.central_widget = QWidget() # define central widget self.setCentralWidget(self.central_widget) self.vbox = QVBoxLayout(self.central_widget) self.vbox.addWidget(self.imgWidget()) self.vbox.addWidget(QPushButton("test")) def imgWidget(self): self.imglabel = QLabel() self.imglabel.setScaledContents(True) self.image = QImage("calib.jpeg") self.imagepix = QPixmap.fromImage(self.image) self.imglabel.setPixmap(self.imagepix) self.imglabel.mousePressEvent = self.imgMousePress return self.imglabel def imgMousePress(self, e): painter = QPainter(self.imglabel.pixmap()) pen = QPen() pen.setWidth(10) pen.setColor(QColor('red')) painter.setPen(pen) painter.drawPoint(e.x(), e.y()) painter.end() self.imglabel.update() if __name__ == '__main__': app = QApplication(sys.argv) mainMenu = Menu() mainMenu.show() sys.exit(app.exec_())**bolded text**
-
Hi and welcome to devnet,
I can't answer to the why but one thing that is sure is that the way you use for the painting does not look correct.
If you really want to customise the painting on your label, you should subclass QLabel and re-implement both paintEvent and mousePressEvent. The former to do the custom painting and the later to store the point you want to paint.