4

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.

4

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.

  • I've got a push button working, but I can't figure out how to create a shortcut for a pushbutton inside a dialog. Any thoughts on how? – Jan Aug 15 '16 at 9:10
1

Does it work if you first connect the signal and then register the MainWindowAction?

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

That's the way its done ine the MultiAttrbuteEdit-Plugin of QGIS where it works to open the dialog with the F12-key:

self.update_selected_action = QAction(icon, "Update field of selected features", self.iface.mainWindow())
QObject.connect(self.update_selected_action, SIGNAL("triggered()"), self.update_selected)
self.iface.registerMainWindowAction(self.update_selected_action, "F12")

https://github.com/lucadelu/QuickMultiAttributeEdit/blob/master/quickmultiattributeedit_menu.py#L31

  • For some reason this doesn't work either - no error given. The shortcut gets registered in the Shortcut Manager but the keypress doesn't open anything. I'm going to try arwork21's suggestion and make a second action button. – Jan Aug 12 '16 at 22:20
  • ...and then connect a shortcut to the new action button. – Jan Aug 12 '16 at 22:29
  • I still can't figure out how to create a shortcut for a pushbutton inside a dialog. – Jan Aug 15 '16 at 9:11
  • F6 also did not work in my case. Can you test something else like self.iface.registerMainWindowAction(self.keyAction, "Ctrl+F12") to be sure it's not a Shortcut that's already registered for something else – markgraeflerland Aug 15 '16 at 9:56
  • No, Ctrl+F12 doesn't work either. It shows up in the shortcut manager, but nothing happens when I try that combination. Other shortcuts that are listed in the shortcut manager work just fine - just not the ones associated with my plugin. Should it even be possible to get a shortcut working for a pushbutton inside a dialog that is already open? – Jan Aug 15 '16 at 21:58

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.