ArekBulski
ArekBulski

Reputation: 5118

PyQt4 mouse move event: find widget

How can I find over which widget is the event fired? The position is relative to origin of whatever widget is underneath it. I want to constrain the event to only one wiget.

def eventFilter(self, source, event):
    if event.type() == QtCore.QEvent.MouseMove:
        if event.buttons() == QtCore.Qt.NoButton:
            pos = event.pos()
            self.statusbar.showMessage("mouse tracked at {} x {}".format(pos.x(), pos.y()))
            print(dir(event))
            # self.edit.setText('x: %d, y: %d' % (pos.x(), pos.y()))
    return QtGui.QMainWindow.eventFilter(self, source, event)

Upvotes: 0

Views: 396

Answers (1)

ekhumoro
ekhumoro

Reputation: 120818

The events that are passed through eventFilter are restricted to the widgets that explicitly installed an event-filter. If only one widget installed an event-filter, the source argument can only ever be that one widget.

If more than one widget installed an event-filter on the same filtering object, you can just use an identity-check to distinguish between them:

def eventFilter(self, source, event):
    if (event.type() == QtCore.QEvent.MouseMove and
        event.buttons() == QtCore.Qt.NoButton and
        source is self.myInterestingWidget):
        # do stuff with event...
        print(event.pos())
    return QtGui.QMainWindow.eventFilter(self, source, event)

Upvotes: 1

Related Questions