ActionManager Class | Extending Qt Creator Manual

- Extending Qt Creator Manual
- ActionManager
- Extending Qt Creator Manual 4.13.0

# ActionManager Class

class Core::ActionManager

The ActionManager class is responsible for registration of menus and menu items and keyboard shortcuts. More...

Header: | #include <coreplugin/actionmanager/actionmanager.h>

- List of all members, including inherited members

## Signals

void | commandAdded(Utils::Id id)
void | commandListChanged()

## Static Public Members

QList<Core::Command *> | commands()
Core::ActionManager * | instance()
bool | isPresentationModeEnabled()
QString | withNumberAccelerator(const QString &text, const int number)

## Detailed Description

The action manager is the central bookkeeper of actions and their shortcuts and layout. It is a singleton containing mostly static functions. If you need access to the instance, for example for connecting to signals, call its ActionManager::instance() function.

The action manager makes it possible to provide a central place where the users can specify all their keyboard shortcuts, and provides a solution for actions that should behave differently in different contexts (like the copy/replace/undo/redo actions).

See The Action Manager and Commands for an overview of the interaction between Core::ActionManager, Core::Command, and Core::Context.

Register a globally active action "My Action" by putting the following in your plugin's ExtensionSystem::IPlugin::initialize() function.

     QAction *myAction = new QAction(tr("My Action"), this);
     Command *cmd = ActionManager::registerAction(myAction, "myplugin.myaction", Context(C_GLOBAL));
     cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+u")));
     connect(myAction, &QAction::triggered, this, &MyPlugin::performMyAction);

The `connect` is done to your own QAction instance. If you create for example a tool button that should represent the action, add the action from Command::action() to it.

     QToolButton *myButton = new QToolButton(someParentWidget);
     myButton->setDefaultAction(cmd->action());

Also use the action manager to add items to registered action containers like the application's menu bar or menus in that menu bar. Register your action via the Core::ActionManager::registerAction() function, get the action container for a specific ID (as specified for example in the Core::Constants namespace) with Core::ActionManager::actionContainer(), and add your command to this container.

Building on the example, adding "My Action" to the "Tools" menu would be done with

     ActionManager::actionContainer(Core::Constants::M_TOOLS)->addAction(cmd);

See also Core::ICore, Core::Command, Core::ActionContainer, Core::IContext, and The Action Manager and Commands.

## Member Function Documentation

### `[signal]` void ActionManager::commandAdded(Utils::Id id)

Emitted when a command (with the id) is added.

### `[signal]` void ActionManager::commandListChanged()

Emitted when the command list has changed.

### `[static]` QList<Core::Command *> ActionManager::commands()

Returns all registered commands.

### `[static]` Core::ActionManager *ActionManager::instance()

Returns the pointer to the instance. Only use for connecting to signals.

### `[static]` bool ActionManager::isPresentationModeEnabled()

Returns whether presentation mode is enabled.

The presentation mode is enabled when starting Qt Creator with the command line argument `-presentationMode`. In presentation mode, Qt Creator displays any pressed shortcut in an overlay box.

### `[static]` QString ActionManager::withNumberAccelerator(const QString &text, const int number)

Decorates the specified text with a numbered accelerator key number, in the style of the Recent Files menu.