Python Pyqt鼠标悬停在QPushButton上

Python Pyqt鼠标悬停在QPushButton上,python,python-2.7,events,pyqt4,Python,Python 2.7,Events,Pyqt4,我想检测鼠标悬停在QPushButton上的情况。为此,我在按钮上安装了一个事件过滤器。但是,当鼠标位于按钮上方时,MouseMove事件不会准确触发。有时,当我在与上一个不同的位置单击按钮时,它似乎会被触发。简言之: 我在按钮上移动鼠标:什么也没发生。 我单击:MouseButtonPressed事件被触发。 我将鼠标移动到按钮上的另一个位置:什么也不发生。 我再次单击:MouseButtonPressed被触发,MouseMove也被触发 我想在每次鼠标悬停按钮时触发MouseMove。我该

我想检测鼠标悬停在
QPushButton
上的情况。为此,我在按钮上安装了一个事件过滤器。但是,当鼠标位于按钮上方时,
MouseMove
事件不会准确触发。有时,当我在与上一个不同的位置单击按钮时,它似乎会被触发。简言之: 我在按钮上移动鼠标:什么也没发生。 我单击:
MouseButtonPressed
事件被触发。 我将鼠标移动到按钮上的另一个位置:什么也不发生。 我再次单击:
MouseButtonPressed
被触发,
MouseMove
也被触发

我想在每次鼠标悬停按钮时触发MouseMove。我该怎么办

这是我的密码:

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

编辑:


事实上,
MouseMove
是在按下
QPushButton
的同时移动鼠标时触发的

我找到了答案。我在搜索一个包含关键字
Mouse
的事件时被误导了。我要查找的事件实际上是
QtCore.QEvent.HoverMove

您还可以使用QPushButton.entereevent和QPushButton.leaveEvent。看一看