QtCreator菜单栏如下:
QtCreator默认菜单包括“文件”、“编辑”、“工具”、“窗体”、“帮助”。“构建”、“调试”、“分析”由插件提供,不是QtCreator的默认菜单。在“帮助”菜单中的“关于插件”对话框中将所有可以取消的插件取消后重启QtCreator,得到QtCreator默认菜单如下:
QtCreator主程序仅仅是一个插件加载器。QtCreator所提供的所有功能都是通过插件实现的。QtCreator最主要的一个插件叫做Core插件。如果没有Core插件,QtCreator将没有插件加载功能。
Core插件的关键组件是ActionManager。ActionManager的作用是注册菜单、菜单项以及键盘快捷键。如果想要添加新的菜单或菜单项,就需要使用ActionManager。
为了访问ActionManager,可以使用下面的代码:
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/icore.h>
Core::ActionManager* am = Core::ICore::instance()->actionManager();
ActionContianer表示QtCreator中的菜单或者菜单栏。通常不会直接创建ActionContainer的实例,而通过ActionManager::createMenu()、ActionManager::createMenuBar()函数进行访问。
QtCreator每一个默认菜单都关联一个ActionContainer对象。给定一个菜单,获取其关联的ActionContainer对象,可以使用下面的代码:
#include <coreplugin/coreconstants.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/icore.h>
Core::ActionManager* am = Core::ICore::instance()->actionManager();
Core::ActionContainer* ac = am->actionContainer(ID);
QtCreator每个菜单的ID如下:
每个ID都是Core命名空间中的静态const char *常量,$$QT_CREATOR_ROOT/src/plugins/coreplugin/coreconstants.h文件中定义了这些常量。
如果想要访问“Help”菜单,可以使用如下的代码:
#include <coreplugin/coreconstants.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/icore.h>
Core::ActionManager* am = Core::ICore::instance()->actionManager();
Core::ActionContainer* ac = am->actionContainer( Core::Constants::M_HELP );
如果将DoNothing插件添加到“Help”菜单,增加为“About DoNothing”菜单项。
DoNothingPlugin.cpp文件修改如下:
#include <coreplugin/coreconstants.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/icore.h>
#include <QMenu>
bool DoNothingPlugin::initialize(const QStringList& args, QString *errMsg)
{
Q_UNUSED(args);
Q_UNUSED(errMsg);
Core::ActionManager* am = Core::ICore::instance()->actionManager();
Core::ActionContainer* ac = am->actionContainer(Core::Constants::M_HELP);
QAction* aboutDoNothing = ac->menu()->addAction(QString::fromUtf8("About DoNothing"));
return true;
}
编译后启动QtCreator,菜单如下:
虽然可以使用上述方法添加菜单项,但并不是推荐的方式。QtCreator中的菜单项必须在Keyboard Shortcuts参数界面中列出。
通过注册菜单项,可以实现QtCreator中的菜单项在Keyboard Shortcuts参数界面中列出。
QtCreator的所有菜单项都应该出现在键盘选择里面的“Keyboard Shortcuts” 中。
Core::Command类表示一个action动作,例如菜单项menu item、工具按钮tool button,或者是快捷键shortcut。开发者不能直接创建Command对象,而是使用ActionManager::registerAction()注册一个 action,然后获取返回值,其返回值就是一个Command。Command对象表示用户可见的action及其属性。
下面代码显示了如何为DoNothing插件注册“About DoNothing” 菜单项:
#include <coreplugin/coreconstants.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/icore.h>
#include <QKeySequence>
#include <QAction>
bool DoNothingPlugin::initialize(const QStringList& args, QString *errMsg)
{
Q_UNUSED(args);
Q_UNUSED(errMsg);
// Fetch the action manager
Core::ActionManager* am = Core::ICore::instance()->actionManager();
// Create a command for "About DoNothing".
Core::Command* cmd = am->registerAction(
new QAction(this),
"DoNothingPlugin.AboutDoNothing",
Core::Context(Core::Constants::C_GLOBAL));
cmd->action()->setText(QString::fromUtf8("About DoNothing"));
// Add the command to Help menu
am->actionContainer(Core::Constants::M_HELP)->addAction(cmd);
return true;
}
编译后运行,About DoNothing菜单项已经出现在Help菜单的开始位置。
使用注册方式添加菜单项,可以在Keyboard Shortcuts参数界面中列出About DoNothing菜单项,并可以关联快捷键。
DoNothing插件已经作为一个菜单项加入到QtCreator中。既然菜单项就是一个QAction,可以通过连接triggered(bool)或者toggled(bool)信号,并增加响应trigger、toggled事件的槽函数。
插件类的实现如下:
DoNothingPlugin.h文件:
#ifndef DONOTHINGPLUGIN_H
#define DONOTHINGPLUGIN_H
#include <extensionsystem/iplugin.h>
class DoNothingPlugin : public ExtensionSystem::IPlugin
{
Q_OBJECT
public:
DoNothingPlugin();
~DoNothingPlugin();
void extensionsInitialized();
bool initialize(const QStringList & arguments, QString * errorString);
void shutdown();
protected slots:
void doNothing();
};
#endif // DONOTHINGPLUGIN_H
DoNothingPlugin.cpp文件:
#include "DoNothingPlugin.h"
#include <QtPlugin>
#include <QStringList>
#include <coreplugin/coreconstants.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/icore.h>
#include <QKeySequence>
#include <QAction>
#include <QMessageBox>
DoNothingPlugin::DoNothingPlugin()
{
// Do nothing
}
DoNothingPlugin::~DoNothingPlugin()
{
// Do notning
}
bool DoNothingPlugin::initialize(const QStringList& args, QString *errMsg)
{
Q_UNUSED(args);
Q_UNUSED(errMsg);
// Fetch the action manager
Core::ActionManager* am = Core::ICore::instance()->actionManager();
// Create a command for "About DoNothing".
Core::Command* cmd = am->registerAction(
new QAction(this),
"DoNothingPlugin.AboutDoNothing",
Core::Context(Core::Constants::C_GLOBAL));
cmd->action()->setText(QString::fromUtf8("About DoNothing"));
// Add the command to Help menu
am->actionContainer(Core::Constants::M_HELP)->addAction(cmd);
connect(cmd->action(), SIGNAL(triggered(bool)), this, SLOT(doNothing()));
return true;
}
void DoNothingPlugin::extensionsInitialized()
{
// Do nothing
}
void DoNothingPlugin::shutdown()
{
// Do nothing
}
void DoNothingPlugin::doNothing()
{
QMessageBox::information(
0,
QString::fromUtf8("About DoNothing Plugin"),
QString::fromUtf8("Seriously dude, this plugin does nothing")
);
}
Q_EXPORT_PLUGIN(DoNothingPlugin)
编译后运行QtCreator,点击DoNothing菜单项:
使用Qt Creator主窗口作为弹出消息对话框的parent,代码修改如下:
void DoNothingPlugin::doNothing()
{
QMessageBox::information(
Core::ICore::instance()->mainWindow(),
"About DoNothing Plugin",
"Seriously dude, this plugin does nothing");
}
向QtCreator添加菜单不能使用Core::Command,而是使用Core::ActionContainer,将其添加到MENU_BAR上面。代码如下:
#include <QMenu>
bool DoNothingPlugin::initialize(const QStringList& args, QString *errMsg)
{
Q_UNUSED(args);
Q_UNUSED(errMsg);
// Fetch the action manager
Core::ActionManager* am = Core::ICore::instance()->actionManager();
// Create a DoNothing menu
Core::ActionContainer* ac = am->createMenu("DoNothingPlugin.DoNothingMenu");
ac->menu()->setTitle(QString::fromUtf8("DoNothing"));
// Create a command for "About DoNothing".
Core::Command* cmd = am->registerAction(
new QAction(this),
"DoNothingPlugin.AboutDoNothing",
Core::Context(Core::Constants::C_GLOBAL));
cmd->action()->setText(QString::fromUtf8("About DoNothing"));
connect(cmd->action(), SIGNAL(triggered(bool)), this, SLOT(doNothing()));
// Add DoNothing menu to the menubar
am->actionContainer(Core::Constants::MENU_BAR)->addMenu(ac);
// Add the "About DoNothing" action to the DoNothing menu
ac->addAction(cmd);
return true;
}
编译运行QtCreator如下:
当前添加的菜单或者菜单项都是在第一个位置。如何修改新增菜单或菜单项的位置,将“DoNothing”菜单放到“Help”前。示例代码如下:
#include <QMenuBar>
bool DoNothingPlugin::initialize(const QStringList& args, QString *errMsg)
{
Q_UNUSED(args);
Q_UNUSED(errMsg);
Core::ActionManager* am = Core::ICore::instance()->actionManager();
// Create a DoNothing menu
Core::ActionContainer* ac = am->createMenu("DoNothingPlugin.DoNothingMenu");
ac->menu()->setTitle(QString::fromUtf8("DoNothing"));
// Create a command for "About DoNothing".
Core::Command* cmd = am->registerAction(
new QAction(this),
"DoNothingPlugin.AboutDoNothing",
Core::Context(Core::Constants::C_GLOBAL));
cmd->action()->setText(QString::fromUtf8("About DoNothing"));
connect(cmd->action(), SIGNAL(triggered(bool)), this, SLOT(doNothing()));
// Insert the "DoNothing" menu between "Window" and "Help".
QMenu* helpMenu = am->actionContainer(Core::Constants::M_HELP)->menu();
QMenuBar* menuBar = am->actionContainer(Core::Constants::MENU_BAR)->menuBar();
menuBar->insertMenu(helpMenu->menuAction(), ac->menu());
// Add the "About DoNothing" action to the DoNothing menu
ac->addAction(cmd);
return true;
}
编译运行QtCreator如下:
可以使用相同的方法定位菜单项的位置。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。