Thursday 23 April 2015

qgis processing - Assigning shortcut to PyQGIS script?


Is it possible to assign a keyboard shortcut to a (custom or not...) script of the Processing Toolbox in QGIS?



I have not found any information about it.



Answer



This is an example of how to open the Processing "Join attributes" algorithm by pressing Ctrl + 1 (you can copy and paste it into the QGIS Python console):


# Function to open the "Join attributes" algorithm's UI
# See http://gis.stackexchange.com/questions/156633/how-to-launch-processing-tool-user-interface-using-pyqgis
from processing.core.Processing import Processing
from processing.gui.CommanderWindow import CommanderWindow
cw = CommanderWindow(iface.mainWindow(), iface.mapCanvas())
def openAlgorithm():
alg = Processing.getAlgorithm("qgis:joinattributestable")

if alg is not None:
cw.runAlgorithm(alg)

# Assign "Ctrl+1" to openAlgorithm()
from PyQt4.QtGui import QShortcut, QKeySequence
from PyQt4.QtCore import Qt
shortcut = QShortcut(QKeySequence(Qt.ControlModifier + Qt.Key_1), iface.mainWindow())
shortcut.setContext(Qt.ApplicationShortcut)
shortcut.activated.connect(openAlgorithm)


That's it! If you press Ctrl + 1 the Join Attributes UI will open:


enter image description here


Note 1: You can get the names of the available algorithms by entering these lines in the QGIS Python console:


import processing
processing.alglist()

Note 2: See Qt4 docs for a comprehensive list of keys.


Note 3: You can call shortcut.activated.disconnect(openAlgorithm) to finish the association between the shortcut and your algorithm's UI.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...