【发布时间】:2015-02-14 23:53:43
【问题描述】:
我想检测鼠标悬停在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 的同时移动鼠标时触发的。
【问题讨论】:
标签: python python-2.7 events pyqt4