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

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.tr(u'&Copy Attributes'),
    # remove the toolbar
    del self.toolbar

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:


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

self.keyAction = QAction(u"Copy the Attributes", self.iface.mainWindow())
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.

3 Answers 3


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

def initGui(self):
    # more stuff here not shown

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, 2016 at 9:10

Correct me if I'm wrong.

I don't think it's possible to add an action in a custom plugin as a viable shortcut by using registerMainWindowAction(). Following @markgraeflerland suggestion, I can see the shortcut displayed in the QGIS Keyboard Shortcuts window, but it doesn't work no matter how I set it.

Having a look at the pyqgis cookbook , it said to add a shortcut in the initGui() method.

self.key_action = QAction("Test Plugin", self.iface.mainWindow())
self.iface.registerMainWindowAction(self.key_action, "Ctrl+I")  
self.iface.addPluginToMenu("&Test plugins", self.key_action) # won't work without calling this method

def key_action_triggered(self):
  QMessageBox.information(self.iface.mainWindow(),"Ok", "You pressed Ctrl+I")

And I found the shortcut won't work if I don't call self.iface.addPluginToMenu().

Therefore, I guess registerMainWindowAction() is only suitable for the actions displaying on the QGIS main UI, like custom plugin icons, Selection tool, etc.

To add shortcuts to a custom plugin, there are two ways working for me:

  1. self.pushButton.setShortcut('Ctrl+Y') from @artwork21's answer.
  shortcut = QShortcut(QKeySequence('Ctrl+Y'), iface.mainWindow())

  def test():
      print('test shortcut')

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.iface.registerMainWindowAction(self.keyAction, "F6")

That's the way it's done in 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")


  • 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, 2016 at 22:20
  • ...and then connect a shortcut to the new action button.
    – Jan
    Aug 12, 2016 at 22:29
  • I still can't figure out how to create a shortcut for a pushbutton inside a dialog.
    – Jan
    Aug 15, 2016 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
    – Thomas B
    Aug 15, 2016 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, 2016 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.