Skip to content


Don't re-register an already registered action in QgsShortcutsManager
Browse files Browse the repository at this point in the history
Avoids incorrect warnings about duplicate shortcuts on startup.

What's happening here is:
- on QGIS startup, plugins are loaded, adding their actions to
the interface via iface.registerMainWindowAction()
- after ALL plugins and qgis native menus and actions are created,
the shortcut manager registers ALL children from the main window.
This includes the actions and widgets created by plugins, which
have already been registered to the manager.
- There's no way to avoid this duplicate registration - we could
move the child shortcut registration to occur before plugin
initialization, but it's actually nice to have this "catch-all"
occur after plugins are loaded (so that plugins which don't
correctly register actions still have them included in the shortcut
manager). Similarly, plugins MUST use the registerMainWindowAction
call instead of just relying on the Qt QAction.setShortcut method
because otherwise the shortcuts manager is unaware of actions
created after QGIS load - e.g. enabling a plugin after startup.

So we avoid this by just refusing to re-register a shortcut
that the manager is already aware of... no more startup warnings!
  • Loading branch information
nyalldawson committed Jan 24, 2018
1 parent 63db1be commit c41b2dd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/gui/qgsshortcutsmanager.cpp
Expand Up @@ -77,6 +77,9 @@ void QgsShortcutsManager::registerAllChildShortcuts( QObject *object, bool recur

bool QgsShortcutsManager::registerAction( QAction *action, const QString &defaultSequence )
if ( mActions.contains( action ) )
return false; // already registered

// if using a debug build, warn on duplicate actions
if ( actionByName( action->text() ) || shortcutByName( action->text() ) )
Expand Down
5 changes: 5 additions & 0 deletions tests/src/python/
Expand Up @@ -90,6 +90,11 @@ def testRegisterAction(self):
action2 = QAction('action2', None)
self.assertTrue(s.registerAction(action2, 'B'))
self.assertCountEqual(s.listActions(), [action1, action2])

# try re-registering an existing action - should fail, but leave action registered
self.assertFalse(s.registerAction(action2, 'B'))
self.assertCountEqual(s.listActions(), [action1, action2])

# actions should have been set to default sequences
self.assertEqual(action1.shortcut().toString(), 'A')
Expand Down

0 comments on commit c41b2dd

Please sign in to comment.