python_pardus
python_pardus

Reputation: 320

How to Catch Hover and Mouse Leave Signal In PyQt5

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?

How can I catch mouse-over button and mouse-leave button, like this:

button = QPushButton(window)
button.clicked.connect(afunction)

Note: I use python3.

Upvotes: 7

Views: 5791

Answers (1)

Daniele Pantaleone
Daniele Pantaleone

Reputation: 2723

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

Upvotes: 8

Related Questions