Skip to content

Commit c41b2dd

Browse files
committed
Don't re-register an already registered action in QgsShortcutsManager
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!
1 parent 63db1be commit c41b2dd

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/gui/qgsshortcutsmanager.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ void QgsShortcutsManager::registerAllChildShortcuts( QObject *object, bool recur
7777

7878
bool QgsShortcutsManager::registerAction( QAction *action, const QString &defaultSequence )
7979
{
80+
if ( mActions.contains( action ) )
81+
return false; // already registered
82+
8083
#ifdef QGISDEBUG
8184
// if using a debug build, warn on duplicate actions
8285
if ( actionByName( action->text() ) || shortcutByName( action->text() ) )

tests/src/python/test_qgsshortcutsmanager.py

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ def testRegisterAction(self):
9090
action2 = QAction('action2', None)
9191
action2.setShortcut('y')
9292
self.assertTrue(s.registerAction(action2, 'B'))
93+
self.assertCountEqual(s.listActions(), [action1, action2])
94+
95+
# try re-registering an existing action - should fail, but leave action registered
96+
self.assertFalse(s.registerAction(action2, 'B'))
97+
self.assertCountEqual(s.listActions(), [action1, action2])
9398

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

0 commit comments

Comments
 (0)