Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Reload paintEvent to draw rect but QLabel can't display pixmap or text



  • I want to draw a rectangle on a picture displayed in a QLabel, so I reload the paintEvent of QLabel. I find that the rect can be display, but the pixmap or text can't. I have add arg__1 in reloaded paintEvent, but it is no use. How to solve it?

            class ImageLabel(QLabel):
    
                def __init__(self, parent: MainWindow):
                    super().__init__()
                    self.rect_1_point = QPoint(0, 0)
                    self.rect_2_point = QPoint(0, 0)
                    self.rect_1_x = 0
                    self.rect_1_y = 0
                    self.rect_2_x = 0
                    self.rect_2_y = 0
                    self.press_flag = False
                    self.painted_flag = False
                    self.main_window = parent
    
                def mouseMoveEvent(self, ev: QMouseEvent):
                    if self.press_flag and self.press_flag:
                        self.rect_2_x = ev.x() - self.x()
                        self.rect_2_y = ev.y() - self.y()
                        self.rect_2_point = ev.pos()
                        self.update()
    
                def mousePressEvent(self, ev: QMouseEvent):
                    if ev.button() == Qt.LeftButton and self.pixmap() is not None:
                        self.rect_1_x = ev.x() - self.x()
                        self.rect_1_y = ev.y() - self.y()
                        self.rect_1_point = ev.pos()
                        self.rect_2_point = ev.pos()
                        self.press_flag = True
    
                def mouseReleaseEvent(self, ev: QMouseEvent):
                    if self.press_flag:
                        self.rect_2_x = ev.x() - self.x()
                        self.rect_2_y = ev.y() - self.y()
                        self.rect_2_point = ev.pos()
                        # self.press_flag = False
    
                def paintEvent(self, arg__1: QPaintEvent):
                    arg__1
                    if self.press_flag and not self.rect_1_point == self.rect_2_point:
                        rect_painter = QPainter(self)
                        pen = QPen()
                        pen.setColor(QColor(Qt.red))
                        rect_painter.setPen(pen)
                        pen.setWidth(1)
                        rect_painter.drawRect(QRect(self.rect_1_point, self.rect_2_point))
    

    If I use arg__1(), there will be an error. I don't know how to solve it.


  • Qt Champions 2019

    @klyjm said in Reload paintEvent to draw rect but QLabel can't display pixmap or text:

    but the pixmap or text can't.

    Can you show how you paint those?



  • @jsulm I don't paint them, I just use setPixmap() and setText() to show them. When override paintEvent in C++, painting rectangle and showing text or pixmap can be in the same time by use QLabel::paintEvent(event). But I find in PySide2, I can't show the pixmap or text. Do you know how to do it?


  • Lifetime Qt Champion

    Hi,

    Like in C++, you have to call the base class paintEvent first if you want to draw on top of it later.



  • @SGaist I don't know how to call it before, now I found using super().paintEvent(arg__1) can solve it.