Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Reload paintEvent to draw rect but QLabel can't display pixmap or text

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

Scheduled Pinned Locked Moved Solved Qt for Python
pyside2qt for pythonpython
5 Posts 3 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    klyjm
    wrote on last edited by klyjm
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • K klyjm

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      0
      • jsulmJ jsulm

        @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?

        K Offline
        K Offline
        klyjm
        wrote on last edited by klyjm
        #3

        @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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          K 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

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

            K Offline
            K Offline
            klyjm
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved