2

I'm writing a standalone QGIS Python app. I've subclassed QgsMapTool and have successfully implemented some selection capabilities. Now I'm trying to implement my own zoom behavior, using the same map tool, but QgsMapCanvas seems to have some built-in zooming behavior. How can disable the built-in zooming and get complete control over the mouse wheel?

class MyMapTool(QgsMapTool):
    ...
    def wheelEvent(self, event):
        new_extent = QgsRectangle(...)
        self.canvas.setExtent(new_extent)
        # Something else is messing with the extent.
        # How can I disable it and use my new_extent?

# Set the tool to the canvas. This is the only Map Tool I use.
tool = MyMapTool(self.canvas)
self.canvas.setMapTool(tool)

[EDIT]: I'm using QGIS 2.18.

1

You need accept this event, and refresh canvas.

Sample:

class MyMapTool(QgsMapTool):
    ...
    def wheelEvent(self, event):
        new_extent = QgsRectangle(...)
        self.canvas.setExtent(new_extent)
        self.canvas.refresh()
        event.accept()
        return

# Set the tool to the canvas. This is the only Map Tool I use.
tool = MyMapTool(self.canvas)
self.canvas.setMapTool(tool)
  • That didn't work for me. Even if I do not change the extent, and simply do self.canvas.refresh(), event.accept(), and return, the map still zooms by the default behavior. – mark78 Apr 29 at 4:27
  • 1
    As a hacky workaround, I can steal the wheelEvent from the canvas with 'self.canvas.wheelEvent = self.myOwnWheelEvent', and do my custom zooming there. – mark78 Apr 29 at 6:43
  • yes, you can also do that – Fran Raga Apr 29 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.