How to Catch Hover and Mouse Leave Signal In PyQt5

前端 未结 1 1350
Happy的楠姐
Happy的楠姐 2020-12-21 02:10

QPushButton has a signal which is named clicked(), and we can catch click events through it. Is there a method or signal which catches hover and leave events?

相关标签:
1条回答
  • 2020-12-21 02:29

    You need to subclass the QPushButton class and reimplement the enterEvent and leaveEvent:

    class Button(QPushButton):
    
        def __init__(self, parent=None):
            super(Button, self).__init__(parent)
            # other initializations...
    
        def enterEvent(self, QEvent):
            # here the code for mouse hover
            pass
    
        def leaveEvent(self, QEvent):
            # here the code for mouse leave
            pass
    

    You can then handle the event locally, or emit a signal (if other widgets needs to react on this event you could use a signal to notify the event to other widgets).

    0 讨论(0)
提交回复
热议问题