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

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

1、QtCreator菜單欄簡介

一、QtCreator菜單簡介

QtCreator菜單欄以下:
QtCreator插件開發(二)——QtCreator菜單和菜單項
QtCreator默認菜單包括「文件」、「編輯」、「工具」、「窗體」、「幫助」。「構建」、「調試」、「分析」由插件提供,不是QtCreator的默認菜單。在「幫助」菜單中的「關於插件」對話框中將全部能夠取消的插件取消後重啓QtCreator,獲得QtCreator默認菜單以下:
QtCreator插件開發(二)——QtCreator菜單和菜單項ide

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

三、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菜單和菜單項
每一個ID都是Core命名空間中的靜態const char *常量,$$QT_CREATOR_ROOT/src/plugins/coreplugin/coreconstants.h文件中定義了這些常量。
若是想要訪問「Help」菜單,可使用以下的代碼:this

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

2、添加菜單項

若是將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中的菜單項必須在Keyboard Shortcuts參數界面中列出。
經過註冊菜單項,能夠實現QtCreator中的菜單項在Keyboard Shortcuts參數界面中列出。3d

3、註冊菜單項

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菜單和菜單項
使用註冊方式添加菜單項,能夠在Keyboard Shortcuts參數界面中列出About DoNothing菜單項,並能夠關聯快捷鍵。
QtCreator插件開發(二)——QtCreator菜單和菜單項code

4、響應菜單項

DoNothing插件已經做爲一個菜單項加入到QtCreator中。既然菜單項就是一個QAction,能夠經過鏈接triggered(bool)或者toggled(bool)信號,並增長響應trigger、toggled事件的槽函數。
插件類的實現以下:
DoNothingPlugin.h文件:orm

#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菜單和菜單項
使用Qt Creator主窗口做爲彈出消息對話框的parent,代碼修改以下:

void DoNothingPlugin::doNothing()
{
    QMessageBox::information(
        Core::ICore::instance()->mainWindow(),
        "About DoNothing Plugin",
        "Seriously dude, this plugin does nothing");
}

5、添加菜單

向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以下:
QtCreator插件開發(二)——QtCreator菜單和菜單項

6、定位菜單、菜單項位置

當前添加的菜單或者菜單項都是在第一個位置。如何修改新增菜單或菜單項的位置,將「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以下:
QtCreator插件開發(二)——QtCreator菜單和菜單項可使用相同的方法定位菜單項的位置。

相關文章
相關標籤/搜索