python - Pyqt 鼠标悬停在 QPushButton 上

标签 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 的同时移动鼠标时触发的。

最佳答案

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

关于python - Pyqt 鼠标悬停在 QPushButton 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27508552/

相关文章:

python - 在 Python 中将 url 请求输出写入 csv

python - Flask api 表示影响所有 View

Python Pandas : Inconsistent behaviour of boolean indexing on a Series using the len() method

python - 后台进程锁定 GUI Python

python - 将 .ui 文件转换为 .py 文件时出错

function - 在函数中指定事件类型的优点?,ActionScript 3

javascript - DOM 事件是否会在它们绑定(bind)到的元素消亡后销毁?

python - 使用 Rest-framework 以 CSV 格式导出巨大的 django 查询集

c# - 当变量更改时生成事件以从代码类发送以形成类 c#

Python 对象持久化