您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。