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

QLabel When Clicking, behind widget triggers too



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



  • @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()
    

  • Lifetime Qt Champion

    Accept the event.



  • @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.


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    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.



  • @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()
    

Log in to reply