Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel paintEvent only Pixmap area
QtWS25 Last Chance

QLabel paintEvent only Pixmap area

Scheduled Pinned Locked Moved Unsolved General and Desktop
painteventcustom widgetrectangle
7 Posts 3 Posters 1.2k Views
  • 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.
  • E Offline
    E Offline
    Emrecp
    wrote on 12 Jul 2021, 14:41 last edited by
    #1

    Hello,
    I have rounded image file, using setPixmap function to set image to QLabel.
    Everythings work fine but I need to custom paintEvent.

    As you can see in picture;
    There is a gray area outside the picture, i don't want this. I want this gray area only in picture.
    BIR.png

    Example:
    IKI.png

    My PaintEvent:

        def paintEvent(self, event):
            QLabel.paintEvent(self, event)
            painter = QPainter(self)
            painter.setRenderHint(painter.Antialiasing)
            painter.setBrush(QBrush(Qt.black, Qt.SolidPattern))
            painter.setOpacity(0.15)
            path = QPainterPath()
            path.setFillRule(Qt.WindingFill)
            painter.drawRect(QRect(self.width()*self.Percentage/100,0, self.width(),self.height()))
            painter.setOpacity(1)
            path.closeSubpath()
            painter.setClipPath(path)
            painter.drawPath(path.simplified())
    
    

    Thanks :)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 12 Jul 2021, 19:02 last edited by
      #2

      Hi,

      Does that image have a transparent background ?

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

      E 1 Reply Last reply 12 Jul 2021, 19:27
      1
      • S SGaist
        12 Jul 2021, 19:02

        Hi,

        Does that image have a transparent background ?

        E Offline
        E Offline
        Emrecp
        wrote on 12 Jul 2021, 19:27 last edited by
        #3

        @SGaist Yes image have a transparent background.
        My goal is; draw gray to image when mouse is not clicking. While mouse is clicking this gray should be gone step by step.
        I can do this with mousePressEvent, mouseReleaseEvent.
        But on this paintEvent I couldn't hide this gray area out of image.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 12 Jul 2021, 19:39 last edited by
          #4

          There's one thing not clear.

          Do you have your image shown properly by default ? So without any border for the transparent background ?

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

          E 1 Reply Last reply 12 Jul 2021, 21:21
          0
          • J Offline
            J Offline
            JoeCFD
            wrote on 12 Jul 2021, 19:44 last edited by
            #5

            Setting tranparent to your label background will do it. No extra paint is needed.

            1 Reply Last reply
            0
            • S SGaist
              12 Jul 2021, 19:39

              There's one thing not clear.

              Do you have your image shown properly by default ? So without any border for the transparent background ?

              E Offline
              E Offline
              Emrecp
              wrote on 12 Jul 2021, 21:21 last edited by Emrecp 7 Dec 2021, 21:25
              #6

              @SGaist Yes It shows fine default (without paintEvent).
              But I am trying to do holding button (needs to hold for x second to activate);
              Video: streamable

              Everything works fine but I need to remove this gray area outside of image.
              I can't use constant border radius because some images may be have not border radius.
              So I need to draw this gray color to only showed area ( not all rectangle )

              All my code:

              # -*- coding: utf-8 -*-
              from PySide6 import QtCore, QtGui, QtWidgets
              from PySide6.QtWidgets import QWidget, QLabel, QApplication, QPushButton
              from PySide6.QtCore import QSize, Qt, Signal,QRect, QPoint, QTimer, QPropertyAnimation, QRectF
              from PySide6.QtGui import QColor, QFont, QPixmap, QPainter,QPen, QBrush, QPainterPath, QLinearGradient, QIcon
              import time
              
              class HoldButton(QLabel):
                  Text=""
                  MaxHoldSecond=0.5
                  PressedTimeMS = 0
                  Percentage = 0 # [0 - 100]
                  def __init__(self, Text, PixmapPath=None, MaxHoldSecond=0.5, parent=None):
                      super(HoldButton, self).__init__(parent)
                      self.Text, self.MaxHoldSecond, self.parent = Text, MaxHoldSecond, parent
                      self.setText(Text)
                      self.setFont(QFont('Exo 2', 14))
                      self.setAlignment(Qt.AlignCenter)
                      if PixmapPath != None:
                          self.setPixmap(QPixmap(PixmapPath))
                      self.timer_button = QTimer(self)
              
                      self.timer_button.timeout.connect(self.TmrEvent)
                  def TmrEvent(self):
                      self.PressedTimeMS += 0.001
                      self.Percentage += self.PressedTimeMS / max(self.MaxHoldSecond, 0.1)*10
                      self.Percentage = min(100, max(0, self.Percentage))
                      print("still left clicking...")
                      self.repaint()
              
                  def paintEvent(self, event):
                      QLabel.paintEvent(self, event)
                      painter = QPainter(self)
                      painter.setRenderHint(painter.Antialiasing)
                      painter.setBrush(QBrush(Qt.black, Qt.SolidPattern))
                      painter.setOpacity(0.15)
                      path = QPainterPath()
                      path.setFillRule(Qt.WindingFill)
                      painter.drawRect(QRect(self.width()*self.Percentage/100,0, self.width(),self.height()))
                      painter.setOpacity(1)
                      path.closeSubpath()
                      painter.setClipPath(path)
                      painter.drawPath(path.simplified())
              
                  _Pressing = False
                  def mousePressEvent(self, event):
                      if event.button()!=Qt.LeftButton:return
                      self._Pressing=True
                      print("left click start")
                      self.timer_button.start(10)
              
                      QLabel.mousePressEvent(self, event)
              
                  def mouseReleaseEvent(self, event):
                      if event.button() != Qt.LeftButton: return
                      self._Pressing=False
                      print("left click end")
                      print("Button held for: %f"% self.PressedTimeMS)
                      self.timer_button.stop()
                      self.PressedTimeMS=0
                      def tmrZeroPercentageSlowly(tmr):
                          if self.Percentage > 0:
                              self.Percentage -= 1
                              self.repaint()
                          else:
                              tmr.stop()
                              tmr.deleteLater()
                          if self._Pressing:
                              tmr.stop()
                              tmr.deleteLater()
                          self.Percentage = min(100, max(0, self.Percentage))
                      if self.Percentage == 100:
                          def ToZero():
                              while self.Percentage > 0:
                                  self.Percentage -= 2
                                  self.repaint()
                                  time.sleep(0.001)
              
                          QTimer.singleShot(1, ToZero)
                          self.repaint()
                          self.Successful()
                          return
              
                      tmrSlow = QTimer(self)
                      tmrSlow.timeout.connect(lambda: tmrZeroPercentageSlowly(tmrSlow))
                      tmrSlow.start(10)
                      QLabel.mouseReleaseEvent(self, event)
              
                  def Successful(self):
                      print("Successful")
              
              1 Reply Last reply
              0
              • E Offline
                E Offline
                Emrecp
                wrote on 29 Jul 2021, 17:46 last edited by
                #7

                Any idea?

                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