ActionManager Class
class Core::ActionManagerThe ActionManager class is responsible for registration of menus and menu items and keyboard shortcuts. More...
Header: | #include <coreplugin/actionmanager/actionmanager.h> |
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::tr("My Action"), this); Command *cmd = ActionManager::registerAction(myAction, "myplugin.myaction", Context(C_GLOBAL)); cmd->setDefaultKeySequence(QKeySequence(Tr::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.