Pyqt鼠标悬停在QPushButton上

时间:2014-12-16 15:52:03

标签: python python-2.7 events pyqt4

我想检测鼠标悬停在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

1 个答案:

答案 0 :(得分:3)

我找到了答案。当我在搜索包含关键字Mouse的事件时,我被误导了。我正在寻找的事件实际上是QtCore.QEvent.HoverMove

相关问题