[英]eventFilter on a QWidget with PyQt4
I have a QMainWindow which contains a DrawingPointsWidget
. 我有一个QMainWindow,其中包含
DrawingPointsWidget
。 This widget draws red points randomly. 此小部件随机绘制红点。 I display the mouse coordinates in the QMainWindow's status bar by installing an event filter for the MouseHovering event using
self.installEventFilter(self)
and by implementing the eventFilter()
method . 通过使用
self.installEventFilter(self)
为MouseHovering事件安装事件过滤器并实现eventFilter()
方法,可以在QMainWindow的状态栏中显示鼠标坐标。 It works. 有用。 However I want to get the mouse coordinates on this red-points widget, and not on the QMainWindow.
但是我想在这个红点小部件上获取鼠标坐标,而不是QMainWindow上。 So I want the status bar to display [0, 0] when the mouse is at the top-left corner of the points widget, and not of the QMainWindow.
因此,我希望当鼠标位于点窗口小部件而不是QMainWindow的左上角时,状态栏显示[0,0]。 How do I do that?
我怎么做? I tried
self.installEventFilter(points)
but nothing happens. 我尝试了
self.installEventFilter(points)
但什么都没有发生。
You wil find below a working chunck of code. 您会在下面找到有效的代码块。
It seems that if I write points.installEventFilter(self)
, the QtCore.Event.MouseButtonPressed
event is detected, only the HoverMove
is not. 看来,如果我编写了
points.installEventFilter(self)
,则会检测到QtCore.Event.MouseButtonPressed
事件,而不会检测到HoverMove
。 So the HoverMove
event is not detected on my DrawingPointsWidget
which is a QWidget
. 所以
HoverMove
没有在我的检测到的事件DrawingPointsWidget
这是一个QWidget
。 Surprisingly, the HoverMove
event is detected on the QPushButton
which is a QAbstractButton
which is a QWidget
too! 令人惊讶的是,在
QPushButton
上检测到HoverMove
事件, QPushButton
也是QAbstractButton
,也是QWidget
! I need to write button.installEventFilter(self)
我需要写
button.installEventFilter(self)
import sys
import random
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
class MainWindow(QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.__setUI()
def __setUI(self, appTitle="[default title]"):
self.statusBar()
mainWidget = QWidget()
vbox = QVBoxLayout()
button = QPushButton("Hello")
vbox.addWidget( button )
points = DrawingPointsWidget()
vbox.addWidget(points)
mainWidget.setLayout(vbox)
self.setCentralWidget(mainWidget)
self.installEventFilter(self)
def eventFilter(self, object, event):
if event.type() == QtCore.QEvent.HoverMove:
mousePosition = event.pos()
cursor = QtGui.QCursor()
self.statusBar().showMessage(
"Mouse: [" + mousePosition.x().__str__() + ", " + mousePosition.y().__str__() + "]"
+ "\tCursor: [" + cursor.pos().x().__str__() + ", " + cursor.pos().y().__str__() + "]"
)
return True
elif event.type() == QtCore.QEvent.MouseButtonPress:
print "Mouse pressed"
return True
return False
class DrawingPointsWidget(QWidget):
""
def __init__(self):
super(QWidget, self).__init__()
self.__setUI()
def __setUI(self):
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('Points')
self.show()
def paintEvent(self, e):
"Re-implemented method"
qp = QtGui.QPainter()
qp.begin(self)
self.drawPoints(qp)
qp.end()
def drawPoints(self, qp):
qp.setPen(QtCore.Qt.red)
"Need to get the size in case the window is resized -> generates a new paint event"
size = self.size()
for i in range(1000):
x = random.randint(1, size.width()-1 )
y = random.randint(1, size.height()-1 )
qp.drawPoint(x, y)
def main():
app = QApplication(sys.argv)
#window = WidgetsWindow2()
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Firstly, the event filter needs to be set by the object you want to watch: 首先,事件过滤器需要由您要观看的对象设置:
points.installEventFilter(self)
Secondly, the event you need to listen for is MouseMove
not HoverMove
: 其次,您需要监听的事件是
MouseMove
而不是HoverMove
:
if event.type() == QtCore.QEvent.MouseMove:
Finally, you need to enable mouse-tracking on the target widget: 最后,您需要在目标窗口小部件上启用鼠标跟踪:
class DrawingPointsWidget(QWidget):
def __init__(self):
super(QWidget, self).__init__()
self.setMouseTracking(True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.