QtCreator插件开发(二)——QtCreator菜单和菜单项

QtCreator插件开发(二)——QtCreator菜单和菜单项

一、QtCreator菜单栏简介

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

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

可以使用相同的方法定位菜单项的位置。

原文地址:http://blog.51cto.com/9291927/2105455

时间: 2024-07-28 12:28:27

QtCreator插件开发(二)——QtCreator菜单和菜单项的相关文章

在Unity3d编辑器中加入菜单以及菜单项

在引用UZGUI插件时,u3d编辑器的菜单条发生了变化,新增了菜单和菜单项,于是乎自己也像尝试一下,看了EZGUI的About_EZ_GUI脚本文件后,结果大出我所料,原来SO EASY! 1 using UnityEngine; 2 using UnityEditor; 3 4 public class Example { 5 6 [MenuItem("new Menu/new/new new/new item")] 7 static void showDialog() 8 { 9

java学习:AWT组件和事件处理的笔记(1)--菜单条,菜单,菜单项

菜单放在菜单条里,菜单项放在菜单里1.MenuBar    在java.awt包中,负责创建菜单条,即MenuBar的一个实例,便是一个菜单条.    在Frame类中的setMenuBar(MenuBar bar)方法,可把菜单条添加到窗口的顶端,但只能向窗口中添加一个菜单条2.Menu    在java.awt包中,负责创建菜单,即Menu的一个实例,便是一个菜单.    Meun类的方法如下:            Menu()   建立一个空标题的菜单            Menu(S

QtCreator插件开发(三)——QtCreator架构

QtCreator插件开发(三)--QtCreator架构 一.QtCreator架构简介 QtCreator的核心就是一个插件加载器,其所有功能都是通过插件实现的.QtCreator架构如下:QtCreator的核心功能由Core Plugin (Core::ICore)实现.插件管理器(ExtensionSystem::PluginManager)对插件协作提供了简单方式,允许插件为其他插件扩展提供钩子.PluginManager负责插件的加载,管理,销毁等工作.Core插件是QtCreat

Delphi通过AppendMenu和DeleteMenu在系统菜单中添加删除菜单项

Delphi在系统菜单中添加删除系统菜单项 Delphi在系统菜单中添加删除系统菜单项,利用Windows提供的API函数GetSystemMenu可以得到窗口的系统菜单句柄,再通过AppendMenu和DeleteMenu就可以添加和删除菜单了. 工具/原料 Delphi7 Windows电脑 方法/步骤 打开Delphi7,创建新的工程,默认新窗体的Name属性为Form1   在Form1的Object Inspector中Events里双击OnCreate为Form1创建OnCreate

QtCreator插件开发(一)——QtCreator插件实例

QtCreator插件开发(一)--QtCreator插件实例 版权声明:本系列文章翻译自:Writing Qt Creator Plugins.如果任何人或机构对于版权有异议,请联系我.本文将使用QtCreator-2.8.1版本进行插件开发,由于QtCreator-2.8.1的插件机制进行了部分更改,因此将根据QtCreator-2.8.1插件机制为基础撰写本文. 一.QtCreator源码编译 1.构建目录的创建 QtCreator工程的源码编译推荐在独立工作目录进行,避免源码被污染.在Q

删除桌面右键菜单中无用的菜单项

删除桌面右键菜单中无用的菜单项:使用regedit打开注册表,然后打开HKEY_CLASSES_ROOT\Directory\Background\shell键项,在其中找到你想要删除的菜单,删除即可. 其他右键菜单项的删除:http://jingyan.baidu.com/article/86112f13342b64273797879c.html 电脑左下角:开始——运行“打开”框中键入“regedit”步骤/方法一: 开始——运行里面输入: regsvr32 /u igfxpph.dll 确

Delphi在系统菜单中添加菜单项

unit dy219; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private procedure sysmenu(var msg: twmmenuselect);message wm_syscommand;

很酷的伸缩导航菜单效果,可自定义样式和菜单项。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

Delphi 在系统菜单中添加菜单项

1 unit Unit001; 2 3 interface 4 5 uses 6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs; 8 9 type 10 TForm1 = class(TForm) 11 procedure FormCreate(Sender:TObject