Reputation: 3278
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.
Upvotes: 6
Views: 13960
Reputation: 3278
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
.
Upvotes: 5