+-
Pyqt鼠标悬停在QPushButton上
我想检测鼠标在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()

编辑:

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

最佳答案
我找到了答案.我在搜索包含关键字Mouse的事件时被误导了.我一直在寻找的事件实际上是QtCore.QEvent.HoverMove.
点击查看更多相关文章

转载注明原文:Pyqt鼠标悬停在QPushButton上 - 乐贴网