QgsShortcutsManager class

Shortcuts manager is a class that contains a list of QActions and QShortcuts that have been registered and their shortcuts can be changed.

QgsShortcutsManager is not usually directly created, but rather accessed through QgsGui::shortcutsManager().

Constructors, destructors, conversion operators

QgsShortcutsManager(QObject* parent = nullptr, const QString& settingsRoot = "/shortcuts/")
Constructor for QgsShortcutsManager.

Public functions

auto actionByName(const QString& name) const -> QAction*
Returns an action by its name, or nullptr if nothing found.
auto actionForSequence(const QKeySequence& sequence) const -> QAction*
Returns the action which is associated for a shortcut sequence, or nullptr if no action is associated.
auto defaultKeySequence(QAction* action) const -> QString
Returns the default sequence for an action.
auto defaultKeySequence(QShortcut* shortcut) const -> QString
Returns the default sequence for a shortcut.
auto listActions() const -> QList<QAction*>
Returns a list of all actions in the manager.
auto listAll() const -> QList<QObject*>
Returns a list of both actions and shortcuts in the manager.
auto listShortcuts() const -> QList<QShortcut*>
Returns a list of shortcuts in the manager.
auto objectDefaultKeySequence(QObject* object) const -> QString
Returns the default sequence for an object (either a QAction or QShortcut).
auto objectForSequence(const QKeySequence& sequence) const -> QObject*
Returns the object (QAction or QShortcut) matching the specified key sequence,.
auto registerAction(QAction* action, const QString& defaultShortcut = QString()) -> bool
Registers an action with the manager so the shortcut can be configured in GUI.
void registerAllChildActions(QObject* object, bool recursive = false)
Automatically registers all QActions which are children of the passed object.
void registerAllChildren(QObject* object, bool recursive = false)
Automatically registers all QActions and QShortcuts which are children of the passed object.
void registerAllChildShortcuts(QObject* object, bool recursive = false)
Automatically registers all QShortcuts which are children of the passed object.
auto registerShortcut(QShortcut* shortcut, const QString& defaultSequence = QString()) -> bool
Registers a QShortcut with the manager so the shortcut can be configured in GUI.
auto setKeySequence(const QString& name, const QString& sequence) -> bool
Modifies an action or shortcut's key sequence.
auto setKeySequence(QAction* action, const QString& sequence) -> bool
Modifies an action's key sequence.
auto setKeySequence(QShortcut* shortcut, const QString& sequence) -> bool
Modifies a shortcuts's key sequence.
auto setObjectKeySequence(QObject* object, const QString& sequence) -> bool
Modifies an object's (either a QAction or a QShortcut) key sequence.
auto settingsPath() const -> QString
Returns the root settings path used to store shortcut customization.
auto shortcutByName(const QString& name) const -> QShortcut*
Returns a shortcut by its name, or nullptr if nothing found.
auto shortcutForSequence(const QKeySequence& sequence) const -> QShortcut*
Returns the shortcut which is associated for a key sequence, or nullptr if no shortcut is associated.
auto unregisterAction(QAction* action) -> bool
Removes an action from the manager.
auto unregisterShortcut(QShortcut* shortcut) -> bool
Removes a shortcut from the manager.

Function documentation

QgsShortcutsManager::QgsShortcutsManager(QObject* parent = nullptr, const QString& settingsRoot = "/shortcuts/")

Constructor for QgsShortcutsManager.

Parameters
parent parent object
settingsRoot root QgsSettings path for storing settings, e.g., "/myplugin/shortcuts". Leave as the default value to store settings alongside built in QGIS shortcuts, but care must be taken to not register actions which conflict with the built in QGIS actions.

QAction* QgsShortcutsManager::actionByName(const QString& name) const

Returns an action by its name, or nullptr if nothing found.

Parameters
name action name. Must match QAction's text.

QAction* QgsShortcutsManager::actionForSequence(const QKeySequence& sequence) const

Returns the action which is associated for a shortcut sequence, or nullptr if no action is associated.

Parameters
sequence shortcut key sequence

QString QgsShortcutsManager::defaultKeySequence(QAction* action) const

Returns the default sequence for an action.

Parameters
action action to return default key sequence for

An empty return string indicates no default sequence.

QString QgsShortcutsManager::defaultKeySequence(QShortcut* shortcut) const

Returns the default sequence for a shortcut.

Parameters
shortcut shortcut to return default key sequence for

An empty return string indicates no default sequence.

QList<QAction*> QgsShortcutsManager::listActions() const

Returns a list of all actions in the manager.

QList<QObject*> QgsShortcutsManager::listAll() const

Returns a list of both actions and shortcuts in the manager.

QList<QShortcut*> QgsShortcutsManager::listShortcuts() const

Returns a list of shortcuts in the manager.

QString QgsShortcutsManager::objectDefaultKeySequence(QObject* object) const

Returns the default sequence for an object (either a QAction or QShortcut).

Parameters
object QAction or QShortcut to return default key sequence for

An empty return string indicates no shortcut.

QObject* QgsShortcutsManager::objectForSequence(const QKeySequence& sequence) const

Returns the object (QAction or QShortcut) matching the specified key sequence,.

Parameters
sequence key sequence to find
Returns object with matching sequence, or nullptr if not found

bool QgsShortcutsManager::registerAction(QAction* action, const QString& defaultShortcut = QString())

Registers an action with the manager so the shortcut can be configured in GUI.

Parameters
action action to register. The action must have a unique text string for identification.
defaultShortcut default key sequence for action
Returns true if action was successfully registered

void QgsShortcutsManager::registerAllChildActions(QObject* object, bool recursive = false)

Automatically registers all QActions which are children of the passed object.

Parameters
object parent object containing actions to register
recursive set to true to recursively add child actions

void QgsShortcutsManager::registerAllChildren(QObject* object, bool recursive = false)

Automatically registers all QActions and QShortcuts which are children of the passed object.

Parameters
object parent object containing actions and shortcuts to register
recursive set to true to recursively add child actions and shortcuts

void QgsShortcutsManager::registerAllChildShortcuts(QObject* object, bool recursive = false)

Automatically registers all QShortcuts which are children of the passed object.

Parameters
object parent object containing shortcuts to register
recursive set to true to recursively add child shortcuts

bool QgsShortcutsManager::registerShortcut(QShortcut* shortcut, const QString& defaultSequence = QString())

Registers a QShortcut with the manager so the shortcut can be configured in GUI.

Parameters
shortcut QShortcut to register. The shortcut must have a unique QObject::objectName() for identification.
defaultSequence default key sequence for shortcut
Returns true if shortcut was successfully registered

bool QgsShortcutsManager::setKeySequence(const QString& name, const QString& sequence)

Modifies an action or shortcut's key sequence.

Parameters
name name of action or shortcut to modify. Must match the action's QAction::text() or the shortcut's QObject::objectName()
sequence new shortcut key sequence

bool QgsShortcutsManager::setKeySequence(QAction* action, const QString& sequence)

Modifies an action's key sequence.

Parameters
action action to modify
sequence new shortcut key sequence

bool QgsShortcutsManager::setKeySequence(QShortcut* shortcut, const QString& sequence)

Modifies a shortcuts's key sequence.

Parameters
shortcut QShortcut to modify
sequence new shortcut key sequence

bool QgsShortcutsManager::setObjectKeySequence(QObject* object, const QString& sequence)

Modifies an object's (either a QAction or a QShortcut) key sequence.

Parameters
object QAction or QShortcut to modify
sequence new shortcut key sequence

QShortcut* QgsShortcutsManager::shortcutByName(const QString& name) const

Returns a shortcut by its name, or nullptr if nothing found.

Parameters
name shortcut name. Must match QShortcut's QObject::objectName() property.

QShortcut* QgsShortcutsManager::shortcutForSequence(const QKeySequence& sequence) const

Returns the shortcut which is associated for a key sequence, or nullptr if no shortcut is associated.

Parameters
sequence shortcut key sequence

bool QgsShortcutsManager::unregisterAction(QAction* action)

Removes an action from the manager.

Parameters
action action to remove
Returns true if action was previously registered in manager and has been removed, or false if action was not previously registered in manager

bool QgsShortcutsManager::unregisterShortcut(QShortcut* shortcut)

Removes a shortcut from the manager.

Parameters
shortcut shortcut to remove
Returns true if shortcut was previously registered in manager and has been removed, or false if shortcut was not previously registered in manager