2

I grabbed code from Assign shortcut to a script? to assign shortcuts for quickly changing to preset scales. When I did it for one shortcut, it was working - but then, when applying several ones it didn't anymore - though the script runs without any errros.. Clues, anyone?

# https://gis.stackexchange.com/questions/168562/assign-shortcut-to-a-script/168739
#====================================
##[User scripts]=group
#====================================
from PyQt4.QtGui import QShortcut, QKeySequence
from PyQt4.QtCore import Qt
from qgis.core import *
from qgis.utils import *
from processing.gui.CommanderWindow import CommanderWindow

canvas = iface.mapCanvas()

def chgScale2500():
    canvas.zoomScale( 2500 )

def chgScale5000():
    canvas.zoomScale( 5000 )

def chgScale10000():
    canvas.zoomScale( 10000 )


#Assign "Ctrl+1", "Ctrl+2", .. to chgScaleX
shortcut1 = QShortcut(QKeySequence(Qt.ControlModifier + Qt.Key_1), iface.mainWindow())
shortcut1.setContext(Qt.ApplicationShortcut)
shortcut1.activated.connect(chgScale2500)

shortcut2 = QShortcut(QKeySequence(Qt.ControlModifier + Qt.Key_2), iface.mainWindow())
shortcut2.setContext(Qt.ApplicationShortcut)
shortcut2.activated.connect(chgScale5000)

shortcut3 = QShortcut(QKeySequence(Qt.ControlModifier + Qt.Key_3), iface.mainWindow())
shortcut3.setContext(Qt.ApplicationShortcut)
shortcut3.activated.connect(chgScale10000)

# for resetting shortcuts
#shortcut1.activated.disconnect(chgScale2500)
#shortcut2.activated.disconnect(chgScale5000)
#shortcut3.activated.disconnect(chgScale10000)
6
  • Interesting, your script works fine for me when I pasted your code into the Python Console. How did you run the script?
    – Joseph
    Apr 13 '16 at 9:33
  • Thanks for trying! I did it via the "Processing Toolbox" -> "Create new script"..
    – Kay
    Apr 13 '16 at 14:17
  • 2.12 Lyon.. Saved correctly.. But I'll try a restart and report!
    – Kay
    Apr 13 '16 at 15:45
  • Darn, it was just something in the cache, after trying several version of the script! A restart of QGIS solved the prob! Thanks a lot!
    – Kay
    Apr 13 '16 at 18:15
  • 1
    @Joseph, would you please put your comment as an answer, so I can check it?
    – Kay
    Apr 13 '16 at 18:16
1

When running scripts, a good idea is to unload all the modules that were imported and remove any dependencies. This seemed to be what was causing your scripts to not work the way they should.

Probably the easiest method to achieve the above is to simply restart QGIS.

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.