使用PyQt4的QWidget上的eventFilter [英] eventFilter on a QWidget with PyQt4

查看:105
本文介绍了使用PyQt4的QWidget上的eventFilter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含DrawingPointsWidget的QMainWindow.此小部件随机绘制红点.通过使用self.installEventFilter(self)为MouseHovering事件安装事件过滤器并实现eventFilter()方法,可以在QMainWindow的状态栏中显示鼠标坐标.有用.但是我想在这个红点小部件上获取鼠标坐标,而不是QMainWindow上.因此,我希望当鼠标位于点窗口小部件而不是QMainWindow的左上角时,状态栏显示[0,0].我怎么做?我尝试了self.installEventFilter(points),但没有任何反应.

I have a QMainWindow which contains a 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 . It works. However I want to get the mouse coordinates on this red-points widget, and not on the 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. How do I do that? I tried self.installEventFilter(points) but nothing happens.

您会在下面的有效代码块中找到

You wil find below a working chunck of code.

似乎我写了points.installEventFilter(self)时,检测到了QtCore.Event.MouseButtonPressed事件,只有HoverMove未被检测到.因此,在我的DrawingPointsWidget(是QWidget)上未检测到HoverMove事件. 令人惊讶的是,在QPushButton上也检测到了HoverMove事件,而QPushButton也是QWidget!我需要写button.installEventFilter(self)

It seems that if I write points.installEventFilter(self), the QtCore.Event.MouseButtonPressed event is detected, only the HoverMove is not. So the HoverMove event is not detected on my DrawingPointsWidget which is a QWidget. Surprisingly, the HoverMove event is detected on the QPushButton which is a QAbstractButton which is a QWidget too! I need to write 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)

第二,您需要监听的事件是MouseMove而不是HoverMove:

Secondly, the event you need to listen for is MouseMove not 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)

这篇关于使用PyQt4的QWidget上的eventFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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