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 When Clicking, behind widget triggers too

QLabel When Clicking, behind widget triggers too

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 638 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.
  • EmrecpE Offline
    EmrecpE Offline
    Emrecp
    wrote on last edited by
    #1

    Hi,
    video
    If you watch video, you will see when i click label; Form click event triggers too. But this not happens for QPushButton. This happens for QLabel.

    Code:

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import QFont
    
    class Window(QMainWindow):
        def __init__(self):
            super(Window, self).__init__()
            self.LBL = QLabel("hi only click me not form!", self)
            self.LBL.resize(QSize(300,50))
            self.LBL.setFont(QFont('Arial',20))
            def Clicked_Form(event):
                if event.button () !=Qt.LeftButton:
                    return
                print("Clicked Form!!!!!")
            self.mousePressEvent = Clicked_Form
            def Clicked_Label(event):
                if event.button () !=Qt.LeftButton:
                    return
                print("Clicked label!")
            self.LBL.mouseReleaseEvent = Clicked_Label
    
    
            self.BTN = QPushButton("hi if you click me, form will not triggered.", self)
            self.BTN.setGeometry(QRect(0,100,300,50))
    
            self.resize(QSize(600,480))
            self.show()
    
    
    def main():
    
        app = QApplication(sys.argv)
        w = Window()
    
    
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

    Is any way to trigger only QLabel not behind widget(main form) of QLabel?
    Thanks.

    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      Sure @Christian-Ehrlicher can write C++ and he's pretty good at it.

      You should rather properly subclass your widget rather than the monkey patching you are currently doing. As @Christian-Ehrlicher already suggested, you should also call the base class implementation to ensure proper propagation or stop it calling accept.

      EmrecpE Offline
      EmrecpE Offline
      Emrecp
      wrote on last edited by
      #7

      @SGaist and @Christian-Ehrlicher Thanks, solved!
      Code:

      import sys
      from PyQt5.QtCore import *
      from PyQt5.QtWidgets import *
      from PyQt5.QtGui import QFont
      
      class Window(QMainWindow):
          def __init__(self):
              super(Window, self).__init__()
              self.LBL = QLabel("hi only click me not form!", self)
              self.LBL.resize(QSize(300,50))
              self.LBL.setFont(QFont('Arial',20))
              def Clicked_Form(event):
                  event.ignore()
                  if event.button () !=Qt.LeftButton:
                      return
                  print("Clicked Form!!!!!")
                  event.setAccepted(False)
              def Clicked_Label_Press(event):
                  event.setAccepted(True)
              def Clicked_Label_Release(event):
                  if event.button () !=Qt.LeftButton:
                      return
                  print("Clicked label! Release")
      
              self.LBL.mousePressEvent = Clicked_Label_Press
              self.LBL.mouseReleaseEvent = Clicked_Label_Release
              self.mousePressEvent = Clicked_Form
      
      
              self.BTN = QPushButton("hi if you click me, form will not triggered.", self)
              self.BTN.setGeometry(QRect(0,100,300,50))
      
              self.resize(QSize(600,480))
              self.show()
      
      
      def main():
          app = QApplication(sys.argv)
          w = Window()
          sys.exit(app.exec_())
      
      if __name__ == '__main__':
          main()
      
      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Accept the event.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        EmrecpE 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          Accept the event.

          EmrecpE Offline
          EmrecpE Offline
          Emrecp
          wrote on last edited by
          #3

          @Christian-Ehrlicher

                  def Clicked_Label(event):            
                      if event.button () !=Qt.LeftButton:
                          return
                      print("Clicked label!")
                      event.accept()
          

          Not works. Because window triggers before than label.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            You have to check for accepted() in Clicked_Form and call the base implementation before (don't know how/if this works with pyhon). Events are propagated this way.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            EmrecpE 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              You have to check for accepted() in Clicked_Form and call the base implementation before (don't know how/if this works with pyhon). Events are propagated this way.

              EmrecpE Offline
              EmrecpE Offline
              Emrecp
              wrote on last edited by
              #5

              @Christian-Ehrlicher Can you write c++ code?

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

                Hi,

                Sure @Christian-Ehrlicher can write C++ and he's pretty good at it.

                You should rather properly subclass your widget rather than the monkey patching you are currently doing. As @Christian-Ehrlicher already suggested, you should also call the base class implementation to ensure proper propagation or stop it calling accept.

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

                EmrecpE 1 Reply Last reply
                1
                • SGaistS SGaist

                  Hi,

                  Sure @Christian-Ehrlicher can write C++ and he's pretty good at it.

                  You should rather properly subclass your widget rather than the monkey patching you are currently doing. As @Christian-Ehrlicher already suggested, you should also call the base class implementation to ensure proper propagation or stop it calling accept.

                  EmrecpE Offline
                  EmrecpE Offline
                  Emrecp
                  wrote on last edited by
                  #7

                  @SGaist and @Christian-Ehrlicher Thanks, solved!
                  Code:

                  import sys
                  from PyQt5.QtCore import *
                  from PyQt5.QtWidgets import *
                  from PyQt5.QtGui import QFont
                  
                  class Window(QMainWindow):
                      def __init__(self):
                          super(Window, self).__init__()
                          self.LBL = QLabel("hi only click me not form!", self)
                          self.LBL.resize(QSize(300,50))
                          self.LBL.setFont(QFont('Arial',20))
                          def Clicked_Form(event):
                              event.ignore()
                              if event.button () !=Qt.LeftButton:
                                  return
                              print("Clicked Form!!!!!")
                              event.setAccepted(False)
                          def Clicked_Label_Press(event):
                              event.setAccepted(True)
                          def Clicked_Label_Release(event):
                              if event.button () !=Qt.LeftButton:
                                  return
                              print("Clicked label! Release")
                  
                          self.LBL.mousePressEvent = Clicked_Label_Press
                          self.LBL.mouseReleaseEvent = Clicked_Label_Release
                          self.mousePressEvent = Clicked_Form
                  
                  
                          self.BTN = QPushButton("hi if you click me, form will not triggered.", self)
                          self.BTN.setGeometry(QRect(0,100,300,50))
                  
                          self.resize(QSize(600,480))
                          self.show()
                  
                  
                  def main():
                      app = QApplication(sys.argv)
                      w = Window()
                      sys.exit(app.exec_())
                  
                  if __name__ == '__main__':
                      main()
                  
                  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