动作管理类
class Core::ActionManagerActionManager 类负责注册菜单、菜单项和键盘快捷键。更多...
头文件 | #include <coreplugin/actionmanager/actionmanager.h> |
继承 | 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() 函数中注册一个全局活动动作 "我的动作"。
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 命名空间中指定的那样)的动作容器,并将您的命令添加到此容器中。
在示例的基础上,要将“我的操作”添加到“工具”菜单中,可以使用以下方法:
ActionManager::actionContainer(Core::Constants::M_TOOLS)->addAction(cmd);
另请参阅 Core::ICore,Core::Command,Core::ActionContainer,Core::IContext,以及动作管理器和命令。
成员函数文档
[静态]
Core::ActionContainer *ActionManager::actionContainer(Utils::Id id)
返回使用 createMenu(),createMenuBar(),createTouchBar() 创建的特定 id 的 ActionContainter 实例。
使用 ID Core::Constants::MENU_BAR
获取主菜单栏。
使用 ID Core::Constants::M_FILE
,Core::Constants::M_EDIT
以及类似常量获取各种默认菜单。
使用 ID Core::Constants::TOUCH_BAR
获取主触摸条。
另请参阅 ActionManager::createMenu() 和 ActionManager::createMenuBar。
[静态]
Core::Command *ActionManager::command(Utils::Id id)
返回使用 registerAction() 创建的特定 id 的 Command 实例。
另请参阅 registerAction。
[信号]
void ActionManager::commandAdded(Utils::Id id)
当添加具有 id 的命令时发出。
[信号]
void ActionManager::commandListChanged()
当命令列表更改时发出。
[静态]
QList<Core::Command *> ActionManager::commands()
返回所有已注册的命令。
[静态]
Core::Command *ActionManager::createCommand(Utils::Id id)
创建一个新的 Command 或返回具有指定 id 的现有 Command。
创建的命令还没有任何与之关联的操作,因此实际上无法触发。但系统知道它,它出现在键盘快捷方式设置中,以后可以为其注册 QActions。如果您已经有了想要注册的 QAction,ID 和上下文,则不需要调用此方法。只需直接调用 registerAction() 即可。
[静态]
Core::ActionContainer *ActionManager::createMenu(Utils::Id id)
创建一个新的菜单操作容器或返回具有指定 id 的现有容器。ActionManager 拥有返回的 ActionContainer。通过 actionContainer() 和 ActionContainer::addMenu() 函数将您的菜单添加到其他菜单或菜单栏中。
参见actionContainer() 和 ActionContainer::addMenu
[静态]
Core::ActionContainer *ActionManager::createMenuBar(Utils::Id id)
创建一个新菜单栏动作容器,或者返回指定 id 的现有容器。返回的 ActionContainer 由 ActionManager 所有。
参见createMenu() 和 ActionContainer::addMenu
[静态]
Core::ActionContainer *ActionManager::createTouchBar(Utils::Id id, const QIcon &icon, const QString &text = QString())
创建一个新的(子)触摸栏动作容器,或者返回指定 id 的现有容器。返回的 ActionContainer 由 ActionManager 管理。
请注意,只能创建一个级别的子触摸栏。子触摸栏将以带有 icon 和 text(可留空)的按钮的形式表示,这将在触摸时打开子触摸栏。
参见actionContainer() 和 ActionContainer::addMenu
[静态]
Core::ActionManager *ActionManager::instance()
返回实例的指针。仅用于连接到信号。
[静态]
bool ActionManager::isPresentationModeEnabled()
返回是否启用了演示模式。
在启动 Qt Creator 时,如果使用命令行参数 -presentationMode
,则启用演示模式。在演示模式中,Qt Creator 在叠加框中显示任何按下的快捷键。
[静态]
Core::Command *ActionManager::registerAction(QAction *action, Utils::Id id, const Core::Context &context = Context(Constants::C_GLOBAL), bool scriptable = false)
使 action 在指定的 id 下被系统所知。
返回表示动作的命令实例,它由 ActionManager 管理并由该实例所拥有。只要有不同的 context,就可以用同一 id 注册多个操作。在这种情况下,触发操作会转发到当前活动上下文注册的 QAction。如果未指定可选的 context 参数,则假定全局上下文。一个可脚本化的动作可以在脚本中调用,而不必用户与之交互。
[静态]
void ActionManager::unregisterAction(QAction *action, Utils::Id id)
从指定的 id 中删除关于 action 的知识。
通常,您无需注销操作。注销操作的唯一有效用例是为表示用户自定义操作的操作,例如对于自定义定位器过滤器。如果用户删除了此类操作,它也必须从动作管理器中注销,以便在快捷方式设置等地方消失。
[静态]
QString ActionManager::withNumberAccelerator(const QString &text, const int number)
以最近文件菜单的样式,将序号快捷键number装饰到指定的text上。
©2024 Qt公司版权所有。本文件中的文档贡献是该相应所有人的版权。提供的文档根据GNU自由文档许可证版本1.3的条款进行授权,如自由软件基金会发布的。Qt及其相关标志是Qt公司在芬兰及全球其它国家的商标。所有其他商标均为其各自所有者的财产。