天天看點

QtCreator插件開發(二)——QtCreator菜單和菜單項QtCreator插件開發(二)——QtCreator菜單和菜單項

QtCreator插件開發(二)——QtCreator菜單和菜單項

一、QtCreator菜單欄簡介

1、QtCreator菜單簡介

QtCreator菜單欄如下:

QtCreator插件開發(二)——QtCreator菜單和菜單項QtCreator插件開發(二)——QtCreator菜單和菜單項

QtCreator預設菜單包括“檔案”、“編輯”、“工具”、“窗體”、“幫助”。“建構”、“調試”、“分析”由插件提供,不是QtCreator的預設菜單。在“幫助”菜單中的“關于插件”對話框中将所有可以取消的插件取消後重新開機QtCreator,得到QtCreator預設菜單如下:

QtCreator插件開發(二)——QtCreator菜單和菜單項QtCreator插件開發(二)——QtCreator菜單和菜單項

2、Core::ActionManager簡介

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();           

3、Core::ActionContainer簡介

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如下:

QtCreator插件開發(二)——QtCreator菜單和菜單項QtCreator插件開發(二)——QtCreator菜單和菜單項

每個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插件開發(二)——QtCreator菜單和菜單項QtCreator插件開發(二)——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菜單的開始位置。

QtCreator插件開發(二)——QtCreator菜單和菜單項QtCreator插件開發(二)——QtCreator菜單和菜單項

使用注冊方式添加菜單項,可以在Keyboard Shortcuts參數界面中列出About DoNothing菜單項,并可以關聯快捷鍵。

QtCreator插件開發(二)——QtCreator菜單和菜單項QtCreator插件開發(二)——QtCreator菜單和菜單項

四、響應菜單項

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菜單項:

QtCreator插件開發(二)——QtCreator菜單和菜單項QtCreator插件開發(二)——QtCreator菜單和菜單項

使用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;
}           

六、定位菜單、菜單項位置

#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;
}           

繼續閱讀