Core::ActionManager Class
class Core::ActionManagerActionManager 类负责注册菜单、菜单项和键盘快捷键。更多内容...
| Header: | #include <coreplugin/actionmanager/actionmanager.h> |
| Inherits: | QObject |
信号
| void | commandAdded(Utils::Id id) |
| void | commandListChanged() |
静态公共成员
| Core::ActionContainer * | actionContainer(Utils::Id id) |
| Core::Command * | command(Utils::Id id) |
| QList<Core::Command *> | commands() |
| Core::Command * | createCommand(Utils::Id id) |
| Core::ActionContainer * | createMenu(Utils::Id id) |
| Core::ActionContainer * | createMenuBar(Utils::Id id) |
| Core::ActionContainer * | createTouchBar(Utils::Id id, const QIcon &icon, const QString &text = QString()) |
| Core::ActionManager * | instance() |
| bool | isPresentationModeEnabled() |
| Core::Command * | registerAction(QAction *action, Utils::Id id, const Core::Context &context = Context(Constants::C_GLOBAL), bool scriptable = false) |
| void | unregisterAction(QAction *action, Utils::Id id) |
| QString | withNumberAccelerator(const QString &text, const int number) |
详细描述
操作管理器是操作及其快捷键和布局的中央管理器。它是一个单例,主要包含静态函数。如果你需要访问该实例(例如为了连接信号),请调用其ActionManager::instance()函数。
操作管理器提供了一个中央位置,供用户指定所有键盘快捷键,并为在不同上下文中应表现出不同行为的操作(如复制、替换、撤销和重做操作)提供了解决方案。
有关 `Core::ActionManager`、`Core::Command` 和 `Core::Context` 之间交互的概述,请参阅《操作管理器与命令》。
通过在插件的ExtensionSystem::IPlugin::initialize() 函数中添加以下代码,注册一个全局有效的操作“My Action”。
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);
connect 操作应针对您自己的QAction 实例进行。例如,如果您创建了一个用于表示该操作的工具按钮,请将其与Command::action() 中的操作关联。
QToolButton *myButton = new QToolButton(someParentWidget); myButton->setDefaultAction(cmd->action());
此外,还可以使用操作管理器将项目添加到已注册的操作容器中,例如应用程序的菜单栏或该菜单栏中的子菜单。通过 `Core::ActionManager::registerAction()` 函数注册您的操作,使用 `Core::ActionManager::actionContainer()` 获取特定 ID 的操作容器(例如在 `Core::Constants` 命名空间中指定的 ID),然后将您的命令添加到该容器中。
基于该示例,若要将“My Action”添加到“Tools”菜单中,可使用以下代码:
ActionManager::actionContainer(Core::Constants::M_TOOLS)->addAction(cmd);
另请参阅 Core::ICore 、Core::Command 、Core::ActionContainer 、Core::IContext 以及《操作管理器与命令》。
成员函数文档
[static] Core::ActionContainer *ActionManager::actionContainer(Utils::Id id)
返回使用createMenu()、createMenuBar()、createTouchBar()为指定的id 创建的ActionContainer实例。
使用 IDCore::Constants::MENU_BAR 检索主菜单栏。
使用 IDCore::Constants::M_FILE 、Core::Constants::M_EDIT 以及类似的常量来获取各种默认菜单。
使用 IDCore::Constants::TOUCH_BAR 获取主触控栏。
另请参阅 ActionManager::createMenu() 和ActionManager::createMenuBar()。
[static] Core::Command *ActionManager::command(Utils::Id id)
返回使用registerAction()为指定的id 创建的Command实例。
另请参阅 registerAction()。
[signal] void ActionManager::commandAdded(Utils::Id id)
在添加一条命令(带有id )时触发。
[signal] void ActionManager::commandListChanged()
当命令列表发生变化时触发。
[static] QList<Core::Command *> ActionManager::commands()
返回所有已注册的命令。
[static] Core::Command *ActionManager::createCommand(Utils::Id id)
创建一个命令,或返回一个带有指定id 的现有命令。
创建的命令目前尚未关联任何操作,因此实际上无法被触发。但系统已识别该命令,它会显示在键盘快捷键设置中,并且稍后可以为其注册 QActions。如果您已经有了要注册的QAction 、ID 和 Context,则无需调用此方法。 只需直接调用registerAction()即可。
[static] Core::ActionContainer *ActionManager::createMenu(Utils::Id id)
创建一个新的菜单操作容器,或返回一个具有指定id 的现有容器。ActionManager 拥有返回的ActionContainer 。可通过actionContainer()和ActionContainer::addMenu()函数将您的菜单添加到其他菜单或菜单栏中。
另请参阅 actionContainer() 和ActionContainer::addMenu()。
[static] Core::ActionContainer *ActionManager::createMenuBar(Utils::Id id)
创建一个新的菜单栏操作容器,或返回一个具有指定id 的现有容器。ActionManager 拥有返回的ActionContainer 。
另请参阅 createMenu() 和ActionContainer::addMenu()。
[static] Core::ActionContainer *ActionManager::createTouchBar(Utils::Id id, const QIcon &icon, const QString &text = QString())
创建一个新的(子)触控栏操作容器,或返回一个具有指定id 的现有容器。ActionManager 拥有返回的ActionContainer 。
请注意,只能创建单层子触控栏。子触控栏将表现为一个带有icon 和text (二者均可留空)的按钮,点击该按钮即可打开子触控栏。
另请参阅 actionContainer() 和ActionContainer::addMenu()。
[static] Core::ActionManager *ActionManager::instance()
返回该实例的指针。仅用于连接信号。
[static] bool ActionManager::isPresentationModeEnabled()
返回是否启用了演示模式。
当使用命令行参数-presentationMode 启动Qt Creator 时,演示模式即被启用。在演示模式下,Qt Creator 会将按下的任何快捷键显示在叠加框中。
[static] Core::Command *ActionManager::registerAction(QAction *action, Utils::Id id, const Core::Context &context = Context(Constants::C_GLOBAL), bool scriptable = false)
将一个action 作为指定id 下的操作告知系统。
返回一个 Command 实例,该实例代表应用程序中的操作,并由该ActionManager 拥有。只要context 不同,即可使用相同的id 注册多个操作。 在这种情况下,触发该操作的请求将转发至当前活动上下文中已注册的QAction 。如果未指定可选的context 参数,则默认使用全局上下文。scriptable 操作可从脚本中调用,而无需用户进行交互。
[static] void ActionManager::unregisterAction(QAction *action, Utils::Id id)
删除指定id 下的action 的相关信息。
通常情况下,您无需注销操作。注销操作的唯一有效用例是针对代表用户可定义操作的情况,例如自定义定位器过滤器。如果用户删除了此类操作,还必须将其从操作管理器中注销,以便使其从快捷方式设置等处消失。
[static] QString ActionManager::withNumberAccelerator(const QString &text, const int number)
为指定的text 添加一个带编号的快捷键number ,样式与Recent Files 菜单中的风格一致。
Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.