一、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 managerCore::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 menuam->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 managerCore::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 menuam->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 managerCore::ActionManager* am = Core::ICore::instance()->actionManager();// Create a DoNothing menuCore::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 menubaram->actionContainer(Core::Constants::MENU_BAR)->addMenu(ac);// Add the "About DoNothing" action to the DoNothing menuac->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 menuCore::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 menuac->addAction(cmd);return true;
}

编译运行QtCreator如下:

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

QtCreator插件开发(二)——QtCreator菜单和菜单项相关推荐

  1. qt添加菜单纯代码_QtCreator插件开发(二)——QtCreator菜单和菜单项

    QtCreator插件开发(二)--QtCreator菜单和菜单项 一.QtCreator菜单栏简介 1.QtCreator菜单简介 QtCreator菜单栏如下: QtCreator默认菜单包括&q ...

  2. 13、Java菜单条、菜单、菜单项

    13.Java菜单条.菜单.菜单项 一般用Java做界面时,都得牵涉到菜单条.菜单.菜单项的设计.菜单项放在菜单里,菜单放在菜单条里,且其字体均可设置. 13.1.菜单条(Menubar) Frame ...

  3. 为Windows右键新建菜单添加菜单项

    [标题]:为Windows右键新建菜单添加菜单项 [时间]:2009-3-4 [摘要]:给新建菜单添加或删除一个菜单项,或者想可以新建自定义后缀文件类型的文件,本文是一个参考.如果"新建&q ...

  4. 创建菜单栏、菜单、菜单项

    目录 1.QMainWindow窗口 1.1.创建菜单栏 1.2.创建菜单 1.3.创建菜单项 1.4.菜单项动起来 1.5.设置菜单项的快捷方式 1.6 .添加分隔符 1.7.设置工具栏 1.8.创 ...

  5. Java菜单(菜单条、菜单和菜单项)

    有两种类型的菜单:下拉式菜单和弹出式菜单.本章只讨论下拉式菜单编程方法.菜单与JComboBox和JCheckBox不同,它们在界面中是一直可见的.菜单与JComboBox的相同之处是每次只可选择一个 ...

  6. Matlab GUI编程技巧(十二):menu创建菜单或菜单项

  7. qt添加菜单纯代码_Qt Creator 插件开发(3):添加菜单项

    本章我们将学习如何向 Qt Creator 的菜单栏添加内容.在上一章插件的基础之上,我们将尝试开发一个比较正式的插件--在菜单栏中可以看到,并且可以相应用户动作等. 在我们开始之前,我们先来看看 Q ...

  8. 菜单Menu和菜单项MenuItem

    一.程序的功能介绍 文件子菜单下面有新建,打开和退出子选项.视图下面有普通子选项. 二.程序分区讲解 (1)在shell窗口中添加菜单条(主菜单)--menu_Bar; 类型是菜单(Menu)样式是菜 ...

  9. 微信公众号的二次开发(二 自定义菜单的创建)

    在上篇<微信公众号的二次开发(一)>我们介绍了微信订阅号的一些坑, 以及微信公众平台开发的一些基本配置.下面我们继续介绍如何通过用户关注等行为.获取用户的openid. 自定义微信公众号菜 ...

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

    本文将使用QtCreator-2.8.1版本进行插件开发,由于QtCreator-2.8.1的插件机制进行了部分更改,因此将根据QtCreator-2.8.1插件机制为基础撰写本文. 一.QtCrea ...

最新文章

  1. android点击展开textview,《Android APP可能有的东西》之UI篇:展开TextView全文
  2. oracle tns 代理配置_OGG实现Oracle到MySQL数据平滑迁移
  3. cornerHarris函数
  4. 一文读懂「Attention is All You Need」| 附代码实现
  5. android+百度lbs云,百度——LBS.云 v2.0——云存储扩展字段——Android
  6. Meeting HDU - 5521
  7. Loopback Address
  8. html img图片等比例缩放_我掏空了各大搜索引擎,整理了HTML图片标签笔记,满满干货...
  9. 为了学习数据库索引,我们花了5000美元
  10. creo管道设计教程_Creo7.0设计探索在管道设计的应用
  11. 如何使用串口来给STM32下载程序
  12. 手机端App出现崩溃常见类型
  13. 【UE4】UE4框选
  14. Java编程笔记9:容器(下)
  15. 【回顾】巨杉数据库中标东莞农商银行非结构化内容管理平台项目
  16. 第一节计算机课要教什么作用,信息技术第一节课要求
  17. Grafana+Prometheus实现Ceph监控和cephfs的基本使用(二)
  18. 计算机it知识,IT行业-计算机基础知识总结
  19. 自由幻想系统不能提供服务器,系统指南-自由幻想召集令-QQ自由幻想官方网站...
  20. Linux设置主机名

热门文章

  1. 创造力公式和成就感公式
  2. 海马玩安卓模拟器linux,Droid4X 0.8.4 海马玩安卓模拟器 安卓的福音
  3. matlab 类型强制转换,关于数据类型强制转换的分析和应用
  4. 单页面网站的优化方法大全
  5. vs2008简体中文正式版序列号和下载地址
  6. REST Assured 5 - Abstraction抽象,隐藏实现
  7. 视频号视频如何下载?
  8. javascript之Math
  9. 美国未来计算机人才需求,对话美国帕森斯前院长:“未来人才需求趋势”
  10. 什么是soft matting方法_建筑师学“交互”有什么意义?零基础如何展开?