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

#include

Core::ActionManager* am = Core::ICore::instance()->actionManager();

3、Core::ActionContainer简介

ActionContianer表示QtCreator中的菜单或者菜单栏。通常不会直接创建ActionContainer的实例,而通过ActionManager::createMenu()、ActionManager::createMenuBar()函数进行访问。

QtCreator每一个默认菜单都关联一个ActionContainer对象。给定一个菜单,获取其关联的ActionContainer对象,可以使用下面的代码:

#include

#include

#include

#include

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

#include

#include

#include

Core::ActionManager* am = Core::ICore::instance()->actionManager();

Core::ActionContainer* ac = am->actionContainer( Core::Constants::M_HELP );

二、添加菜单项

如果将DoNothing插件添加到“Help”菜单,增加为“About DoNothing”菜单项。

DoNothingPlugin.cpp文件修改如下:

#include

#include

#include

#include

#include

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

#include

#include

#include

#include

#include

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

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

#include

#include

#include

#include

#include

#include

#include

#include

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

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

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

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

qt添加菜单纯代码_QtCreator插件开发(二)——QtCreator菜单和菜单项相关推荐

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

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

  2. qt添加菜单纯代码_开始玩qt,使用代码修改设计模式生成的菜单

    之前制作菜单时,不是纯代码便是用设计模式 直接图形化完成. 今天我就是想用代码修改已经存在的菜单项,如果是用代码生成的可以直接调用指针完成: 但通过设计模式完成的没有暴露指针给我,至少我没发现. 在几 ...

  3. [Qt教程] 第28篇 XML(二)使用DOM创建和操作XML文档

    [Qt教程] 第28篇 XML(二)使用DOM创建和操作XML文档 楼主  发表于 2013-5-21 22:00:51 | 查看: 475| 回复: 0 使用DOM创建和操作XML文档 版权声明 该 ...

  4. [Qt教程] 第22篇 数据库(二)编译MySQL数据库驱动

    [Qt教程] 第22篇 数据库(二)编译MySQL数据库驱动 楼主  发表于 2013-5-13 21:28:02 | 查看: 1616| 回复: 12 编译MyQSL数据库驱动 版权声明 该文章原创 ...

  5. [Qt教程] 第32篇 网络(二)HTTP

    [Qt教程] 第32篇 网络(二)HTTP 楼主  发表于 2013-8-28 17:21:28 | 查看: 637| 回复: 8 HTTP 版权声明 该文章原创于作者yafeilinux,转载请注明 ...

  6. Qt Creator与Qt+VS2015调用Python代码的实现

    下面来介绍分别用Qt Creator与Qt+VS2015调用Python代码,首先电脑中要安装Python IDE与Qt.我电脑中安装的是Python37,VS2015+Qt5.8.Qt Creato ...

  7. QT添加菜单栏-工具栏-中心区域-状态栏-dock 示范

    QT添加菜单栏-工具栏-中心区域-状态栏-dock 示范 QT添加菜单栏-工具栏-中心区域-状态栏-dock示范.rar-QT文档类资源-CSDN下载QT添加菜单栏-工具栏-中心区域-状态栏-dock ...

  8. Qt添加QOpcUa类

    目录 一:准备阶段 1.下载安装perl 2.下载安装 git 二:Qt添加QOpcUa 1.打开软件 2.执行以下命令 三:验证安装 一:准备阶段 1.下载安装perl 下载地址:perl下载地址 ...

  9. 2021.4.7 美团买菜后端开发实习生(二面)(含总结)(已oc)

    title: 2021.4.7 美团买菜后端开发实习生(二面) tags: 面经 2021.4.7 美团买菜后端开发实习生(二面) 自我介绍 Java学习时长 final关键字的作用(见下文) 注解有 ...

最新文章

  1. xshell连接Linux、ngix部署
  2. 山西计算机软考知识点,计算机软考考试必备知识点:数据标准化
  3. 距离产生美?k近邻算法python实现
  4. html中某个名称不能修改,解决type=file 文件修改表单 名称不能正常回显的问题
  5. SWTBOK測试实践系列(1) -- 測试在项眼下期的评审投入划算吗?
  6. lavaral中文手册_【laravel7.x中文文档】路由
  7. [bootstrapValidator] - bootstrap的验证工具
  8. docker 容器日志集中 ELK + filebeat
  9. Java XMPP负载测试工具
  10. 一起谈.NET技术,WPF 基础到企业应用系列5——WPF千年轮回2
  11. 【剑指offer】面试题63:股票的最大利润(Java)
  12. 4.安卓基础之Activity跳转动画
  13. LeetCode:67. 二进制求和(python、c++)
  14. php使用curl发送 json数据
  15. php环境搭建及入门
  16. 【C++】(三) MFC入门教程 (VS 2005)
  17. 快速阅读之眼球的训练
  18. 计算机的基本组成和工作原理
  19. FlutterComponent最佳实践之沉浸式
  20. 字节、腾讯、金山wps、跟谁学、百度 go工程师面试题集锦

热门文章

  1. 开启Cisco交换机DHCP Snooping功能
  2. Android开发人员必备的10 个开发工具
  3. 如何建议一个数据库内的定时任务
  4. [LeetCode] Majority Element II
  5. 又双叒叕是一个动态简历
  6. [三] java虚拟机 JVM字节码 指令集 bytecode 操作码 指令分类用法 助记符
  7. 延迟任务调度系统—技术选型与设计(上篇)
  8. linux下清空c++ cin无效流的方式
  9. Android开发周报:Google 推出AR SDK、Android 8.0 Oreo 最终版发布
  10. EEPlat 主子表和对象引用配置实例