Skip to main content
added 2 characters in body
Source Link

You need to install the event filter on the canvas' viewport. Event filters do not work as expected with mouse events on classes derived from QAbstractScrollArea (QgsMapCanvas, QGraphicsView, QTextEditQTextEdit, etc...)

Replace your code with the following, and it should work.

class MyPlugin:
    def __init__(self, iface: QgisInterface):
        self.iface = iface
        self.eventFilter: MyEventFilter = None

    def initGui(self):
        self.registerEventFilter()

    def unload(self):
        self.unregisterEventFilter()

    def registerEventFilter(self):
        if not self.eventFilter:
            self.eventFilter = MyEventFilter(self.iface.mapCanvas())
        self.iface.mapCanvas().viewport().installEventFilter(self.eventFilter)

    def unregisterEventFilter(self):
        if self.eventFilter:
            self.iface.mapCanvas().viewport().removeEventFilter(self.eventFilter)
            self.eventFilter.deleteLater()

You need to install the event filter on the canvas' viewport. Event filters do not work as expected with mouse events on classes derived from QAbstractScrollArea (QgsMapCanvas, QGraphicsView, QTextEdit, etc...)

Replace your code with the following, and it should work.

class MyPlugin:
    def __init__(self, iface: QgisInterface):
        self.iface = iface
        self.eventFilter: MyEventFilter = None

    def initGui(self):
        self.registerEventFilter()

    def unload(self):
        self.unregisterEventFilter()

    def registerEventFilter(self):
        if not self.eventFilter:
            self.eventFilter = MyEventFilter(self.iface.mapCanvas())
        self.iface.mapCanvas().viewport().installEventFilter(self.eventFilter)

    def unregisterEventFilter(self):
        if self.eventFilter:
            self.iface.mapCanvas().viewport().removeEventFilter(self.eventFilter)
            self.eventFilter.deleteLater()

You need to install the event filter on the canvas' viewport. Event filters do not work as expected with mouse events on classes derived from QAbstractScrollArea (QgsMapCanvas, QGraphicsView, QTextEdit, etc...)

Replace your code with the following, and it should work.

class MyPlugin:
    def __init__(self, iface: QgisInterface):
        self.iface = iface
        self.eventFilter: MyEventFilter = None

    def initGui(self):
        self.registerEventFilter()

    def unload(self):
        self.unregisterEventFilter()

    def registerEventFilter(self):
        if not self.eventFilter:
            self.eventFilter = MyEventFilter(self.iface.mapCanvas())
        self.iface.mapCanvas().viewport().installEventFilter(self.eventFilter)

    def unregisterEventFilter(self):
        if self.eventFilter:
            self.iface.mapCanvas().viewport().removeEventFilter(self.eventFilter)
            self.eventFilter.deleteLater()
Source Link

You need to install the event filter on the canvas' viewport. Event filters do not work as expected with mouse events on classes derived from QAbstractScrollArea (QgsMapCanvas, QGraphicsView, QTextEdit, etc...)

Replace your code with the following, and it should work.

class MyPlugin:
    def __init__(self, iface: QgisInterface):
        self.iface = iface
        self.eventFilter: MyEventFilter = None

    def initGui(self):
        self.registerEventFilter()

    def unload(self):
        self.unregisterEventFilter()

    def registerEventFilter(self):
        if not self.eventFilter:
            self.eventFilter = MyEventFilter(self.iface.mapCanvas())
        self.iface.mapCanvas().viewport().installEventFilter(self.eventFilter)

    def unregisterEventFilter(self):
        if self.eventFilter:
            self.iface.mapCanvas().viewport().removeEventFilter(self.eventFilter)
            self.eventFilter.deleteLater()