Pyqt鼠标悬停在QPushButton上 [英] Pyqt Mouse hovering on a QPushButton

查看:174
本文介绍了Pyqt鼠标悬停在QPushButton上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测鼠标在 QPushButton 上的悬停.为此,我在按钮上安装了事件过滤器.但是,当鼠标悬停在按钮上时, MouseMove 事件不会完全触发.当我单击与上一个位置不同的位置上的按钮时,似乎有时会触发该事件.简而言之:我将鼠标移到该按钮上:没有任何反应.我单击: MouseButtonPressed 事件被触发.我将鼠标移到按钮上的另一个位置:什么也没发生.我再次单击: MouseButtonPressed 被触发, MouseMove 也被触发.

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.

我希望每次鼠标悬停按钮时触发MouseMove.我该怎么办?

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

这是我的代码:

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


实际上,在按下 QPushButton 的同时移动鼠标时,会触发 MouseMove .

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

推荐答案

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

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.

这篇关于Pyqt鼠标悬停在QPushButton上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆