[GIS] Change zoom factor of QGIS programmatically with PyQGIS

mouse-wheelpyqgisqgisqgis-pluginszoom

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?

Best Answer

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 )