4

I would like to change the zoom factor of QGIS dynamically without restarting QGIS. The changed settings I would like to use for a plugin would be that it let's me dynamically change the zoom behaviour when I use the mouse wheel for zooming in and out.

I know that I can change the QGIS-Settings like that:

from PyQt4.QtCore import QSettings
s = QSettings() #getting qgis settings
s.setValue("/Qgis/zoom_factor", 1.1)
s.sync()

But this requires a restart of QGIS to really change the zoom behaviour. Is there anything else I could do to make QGIS accept the new settings like it does when I click the "OK"-button in the settings dialog?

6

There is no need to alter settings, you can change the zoom factor for the current QGIS session.

For QGIS <= v2.18 (see Wheel actions enum):

zoomFactor = 3
iface.mapCanvas().setWheelAction( 0, zoomFactor ) # Wheel action, Zoom factor

For upcoming QGIS 3, there is a new method (wheel actions have been removed):

zoomFactor = 3
iface.mapCanvas().setWheelFactor( zoomFactor )
  • 2
    Or use QgsMapCanvas.WheelZoom instead of 0 to make it a bit better readable. Karma points for mentioning the QGIS 3.0 API. – Matthias Kuhn Jan 12 '17 at 15:08
  • This can be also used to switch mouse wheel behavior to "ArcMap" style by setting zoomFactor to below 1, for example: iface.mapCanvas().setWheelFactor( 0.5 ) – Miro Jul 26 at 3:00
  • Also be aware iface.mapCanvas().setWheelFactor() changes all zoom in / zoom out functionality (tools), not only mouse wheel behavior. – Miro Jul 26 at 3:19
1

If you are looking for zoom functions, they are defined in the QgsMapCanvas Class. For example, in your case it could be used something like this:

canvas = qgis.utils.iface.mapCanvas()
canvas.zoomByFactor(1.1)

If I run this code from the Python Console, it zooms the canvas by a factor of 1.1. Does it work for you?

  • thanks but I still want use the mouse wheel for zooming. I just want to dynamically adjust how much it zooms in or out when I use the mousewheel – markgraeflerland Jan 12 '17 at 14:28
  • Sorry, I was distracted. I'm glad you solved your problem. – mgri Jan 12 '17 at 14:43

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.