Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [SOLVED] Cropping an image using QLabel and QPixmap
Qt 6.12 LTS is out! See what's new in the release blog

[SOLVED] Cropping an image using QLabel and QPixmap

Scheduled Pinned Locked Moved Mobile and Embedded
4 Posts 2 Posters 8.1k 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.
  • Q Offline
    Q Offline
    qurban_ali36
    wrote on last edited by
    #1

    Hi,
    I want to take a screenshot of my desktop screen and then crop that image by drawing a rectangle on it. So for I am able to capture the screenshot as QPixmap, set it on QLabel and displayed as a window (window.showMaximized()). I am also able to draw a rectangle on QLabel using mouse events and paintEvent of QLabel.
    The problem is that, when the paintEvent is called to draw the rectangle, the actual image vanishes from the QLabel.
    How can I draw a rectangle on QLabel without loosing the pixmap?
    Here is the code:
    @
    class Label(QLabel):
    def init(self, parent = None):
    super(Label, self).init(parent)
    self.mousePos = QPoint()
    self.mouseDown = False
    self.rect = QRect()
    self.setPixmap(QPixmap.grabWindow(QApplication.desktop().winId()))

    def mousePressEvent(self, event):
        self.mouseDown = True
        self.mousePos = event.pos()
        self.rect.setTopLeft(self.mousePos)
    
    def mouseReleaseEvent(self, event):
        self.rect.setBottomRight(event.pos())
        self.repaint()
        
    def mouseMoveEvent(self, event):
        if self.mouseDown:
            self.rect.setBottomRight(event.pos())
            self.repaint()
        
    def paintEvent(self, event):
        if self.rect.x() > 0:
            painter = QPainter(self)    
            painter.setPen(QPen(Qt.black, 1, Qt.SolidLine));
            painter.drawRect(self.rect);
    

    @

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      for such purposes check QRubberBand class on top (as a child widget) of the label

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qurban_ali36
        wrote on last edited by
        #3

        Thank you very much :) It worked like a charm.
        btw, how to make this post as "solved"? I am new to qt-project.org

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          just edit your first post and prepend [SOLVED] to the title.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          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
          • Unsolved