Wednesday 15 February 2017

qgis - How to enable a keyboard shortcut for a plugin


I've created a QGIS plugin using plugin builder which works just fine when I enable the plugin dialog from the toolbar button or the plugin menu item. The plugin has one dialog with a few combo boxes, and an ok/cancel button. The user makes combobox layer selections, then makes feature selections on the canvas, then presses ok to run some code based on those choices and selections.


However I can't seem to get a keyboard short cut enabled, one that would allow the user to keep the plugin dialog open and a keyboard shortcut that would accomplish the same thing as clicking the OK button but without closing the dialog. I've had a look at this method here, but I couldn't get it to trigger the run function where most of my code sits. My initGUI function looks like this:


self.keyAction = QAction(u"Copy the Attributes", self.iface.mainWindow())

self.iface.registerMainWindowAction(self.keyAction, "F6")
self.keyAction.triggered.connect(self.run)

But that doesn't seem to do anything when I press F6. I've tried setting the shortcut in Settings -> Configure Shortcuts, but that doesn't result in anything happening either. My unload function is also probably wrong as well:


def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(
self.tr(u'&Copy Attributes'),
action)

self.iface.removeToolBarIcon(action)
# remove the toolbar
del self.toolbar
#self.iface.unregisterMainWindowAction(self.keyAction)

I'm struggling to understand if I'm even going in the right direction with this registering and unregistering, if I want a single keyboard shortcut key to mimic the press of the OK button in my plugin dialog - while keeping the dialog box open. Keeping the dialog open and using a shortcut key would allow the user to perform multiple plugin actions on the same layers, but with different feature selections on the canvas.


Any thoughts on where my approach goes wrong?


Update: As suggested below, I've added a push button in the dialog which allows me to just run the code needed without closing the dialog:


self.dlg.pushButton.clicked.connect(self.run)


But is it still correct to try to use a shortcut using


self.keyAction = QAction(u"Copy the Attributes", self.iface.mainWindow())
self.keyAction.triggered.connect(self.run)
self.iface.registerMainWindowAction(self.keyAction, "F6")

...when I'm not using the keyboard shortcut to open the dialog, but rather run the code under self.run ? Because it doesn't work when I try this. I'm missing something obvious here.



Answer



I would suggest getting rid of the default "Ok/Cancel" buttons and creating your own push buttons/event listeners (button may be created in Qt Designer):


code e.g:


def doSomething(self):

# process data

def closeForm(self):
self.dlg.close()

def initGui(self):
# more stuff here not shown
self.dlg.pushButton1.clicked.connect(self.doSomething)
self.dlg.pushButton2.clicked.connect(self.closeForm)
self.dlg.pushButton1.setShortcut("A")


When the user clicks the pushButton1 the data will be processed in the doSomething function and the form will stay open. When the user clicks the pushButton2 the form will close.


Update, added statement above when you click the "A" key it will call clicked event of pushButton1 which will call the doSomething function.


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...