带有PyQt4的QWidget上的eventFilter

时间:2015-01-20 16:28:13

标签: python events mouseevent pyqt4

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

您将在下面找到一个有效的代码。


编辑1

似乎如果我写points.installEventFilter(self),则会检测到QtCore.Event.MouseButtonPressed事件,但只有HoverMove没有。因此,在我的HoverMove DrawingPointsWidget上未检测到QWidget事件。 令人惊讶的是,在HoverMove上检测到QPushButton事件,QAbstractButton也是QWidget!我需要写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()

1 个答案:

答案 0 :(得分:6)

首先,需要将事件过滤器设置为 您要观看的对象:

points.installEventFilter(self)

其次,您需要倾听的事件是MouseMove而不是HoverMove

if event.type() == QtCore.QEvent.MouseMove:

最后,您需要在目标小部件上启用鼠标跟踪:

class DrawingPointsWidget(QWidget):
    def __init__(self):
        super(QWidget, self).__init__()
        self.setMouseTracking(True)