Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyqt Mouse hovering on a QPushButton

I want to detect the hovering of the mouse on a QPushButton. For that I installed an event filter on my button. However the MouseMove event does not trigger exactly when the mouse is over the button. It seems it is sometimes triggered when I click the button on a location which is different from the previous one. To put it simply: I move the mouse on the button: nothing happens. I click: MouseButtonPressed event is triggered. I move the mouse to another location on the button: nothing happens. I click again: MouseButtonPressed is triggered, MouseMove too.

I would like to get the MouseMove triggered each time the mouse hovers the button. How do I do?

Here is my code:

import sys

from PyQt4 import QtCore
from PyQt4.QtGui import *

class eventFilterWindow(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        widget = QWidget()

        button = QPushButton("Trigger event!")
        button.installEventFilter(self)

        hbox = QHBoxLayout()
        hbox.addWidget(button)
        widget.setLayout(hbox)
        self.setCentralWidget(widget)
        self.show()

    def eventFilter(self, object, event):

        if event.type() == QtCore.QEvent.MouseButtonPress:
            print "You pressed the button"
            return True

        elif event.type() == QtCore.QEvent.MouseMove:
            print "C'mon! CLick-meeee!!!"
            return True

        return False

def main():
    app = QApplication(sys.argv)
    #myWindow = EventsWindow()
    window = eventFilterWindow()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

EDIT:

In fact, MouseMove is triggered when the mouse is being moved while the QPushButton is pressed.

like image 993
kaligne Avatar asked Dec 16 '14 15:12

kaligne


1 Answers

I found the answer. I was misled as I was searching an event containing the keyword Mouse in it. The event I was looking for actually is QtCore.QEvent.HoverMove.

like image 125
kaligne Avatar answered Oct 11 '22 01:10

kaligne