Ant*_*rth 10 python pyqt mouseover mouseevent pyqt4
当用户将鼠标移动到GUI上时,我需要捕获,但不是当他们按住鼠标按钮时(这将做一些不同的事情).
我找不到任何方便的方法来做到这一点,除了定期找到鼠标位置并检查它以前的位置......哪个会很糟糕.
只有在按下鼠标左键的同时移动鼠标时才会调用mouseMoveEvent,除非小部件具有"鼠标跟踪"功能.鼠标跟踪对我来说不是一个选项,因为当移动鼠标并按下鼠标左键时,GUI的行为必须不同.
有没有内置的方法来做到这一点?(或者只是任何聪明的想法?)
例如:有没有办法检查是否随时按下了鼠标左键?
或者可以应用于QRect(坐标)的"鼠标悬停"事件?
Muchas gracias.
Windows 7(32)
python 2.7
PyQt4
ekh*_*oro 10
最直接的方法是在qApp上安装事件过滤器:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
widget = QtGui.QWidget(self)
layout = QtGui.QVBoxLayout(widget)
self.edit = QtGui.QLineEdit(self)
self.list = QtGui.QListWidget(self)
layout.addWidget(self.edit)
layout.addWidget(self.list)
self.setCentralWidget(widget)
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseMove:
if event.buttons() == QtCore.Qt.NoButton:
pos = event.pos()
self.edit.setText('x: %d, y: %d' % (pos.x(), pos.y()))
else:
pass # do other stuff
return QtGui.QMainWindow.eventFilter(self, source, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
win = Window()
win.show()
app.installEventFilter(win)
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)