为PyQGIS脚本分配快捷方式?


Answers:


9

这是如何通过按Ctrl+ 打开“处理“加入属性”算法的示例1(您可以将其复制并粘贴到QGIS Python控制台中):

# 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)

而已!如果按Ctrl+ 1,将打开“加入属性”用户界面:

在此处输入图片说明

注意1:您可以通过在QGIS Python控制台中输入以下行来获得可用算法的名称:

import processing
processing.alglist()

注意2:有关密钥的完整列表,请参阅Qt4文档

注意3:您可以调用shortcut.activated.disconnect(openAlgorithm)来完成快捷方式和算法的UI之间的关联。


1
它确实可以确保...
snaileater 2015年

1
谢谢。由于某种原因,我在Qt文档QShortcut中的QtWidgets下看到了,而不是QtGui(Qt4和Qt5都看到了)。对于QGIS3和PyQt5,我必须做:从PyQt5.QtWidgets导入QShortcut
Miro,
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.