Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 709
python_pardus Avatar asked Sep 20 '15 11:09

python_pardus


1 Answers

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

like image 190
Daniele Pantaleone Avatar answered Nov 01 '22 09:11

Daniele Pantaleone