Qt Creator plugin动手实践(2)自己动手写qt creator插件,实现自定义工具栏按钮

简介: Qt Creator plugin动手实践(2)自己动手写qt creator插件,实现自定义工具栏按钮

一、环境准备


自己动手写qt creator插件之前,需要先编译Qt Creator源码。详情见博客:


使用Qt Creator IDE+MSVC2015编译器组合,编译Qt Creator源码4.8.2版本



二、编译器使用MSVC2015 32bit,qt creator源码编译完成,会生成很多exe,lib和dll。我们需要重点关注以下几个lib。因为qt creator插件会依赖它们。


Debug\lib\qtcreator\Aggregationd4.lib


Debug\lib\qtcreator\Cored4.lib


Debug\lib\qtcreator\ExtensionSystemd4.lib


Debug\lib\qtcreator\Utilsd4.lib


Release\lib\qtcreator\Aggregation4.lib


Release\lib\qtcreator\Core4.lib


Release\lib\qtcreator\ExtensionSystem4.lib


Release\lib\qtcreator\Utils4.lib



三、新建qt creator插件工程


参考Qt官方文档 https://doc-snapshots.qt.io/qtcreator-extending/first-plugin.html



image.png

image.png


工程.pro文件设置如下:

DEFINES += FIRECAT_TOOLBAR_LIBRARY
# firecat_Toolbar files
SOURCES += \
        firecat_toolbarplugin.cpp
HEADERS += \
        firecat_toolbarplugin.h \
        firecat_toolbar_global.h \
        firecat_toolbarconstants.h
# Qt Creator linking
## Either set the IDE_SOURCE_TREE when running qmake,
## or set the QTC_SOURCE environment variable, to override the default setting
#isEmpty(IDE_SOURCE_TREE): IDE_SOURCE_TREE = $$(QTC_SOURCE)#必须注释掉这句话
isEmpty(IDE_SOURCE_TREE): IDE_SOURCE_TREE = "D:/temp/qt-creator-opensource-src-4.8.2"#指向Qt Creator源码路径
## Either set the IDE_BUILD_TREE when running qmake,
## or set the QTC_BUILD environment variable, to override the default setting
#isEmpty(IDE_BUILD_TREE): IDE_BUILD_TREE = $$(QTC_BUILD)#必须注释掉这句话
#isEmpty(IDE_BUILD_TREE): IDE_BUILD_TREE = "D:/temp/build_plugins"#必须注释掉这句话
Debug:isEmpty(IDE_BUILD_TREE): IDE_BUILD_TREE = "D:/temp/build_plugins/debug/"#自定义Debug生成路径
Release:isEmpty(IDE_BUILD_TREE): IDE_BUILD_TREE = "D:/temp/build_plugins/release/"#自定义Release生成路径
## uncomment to build plugin into user config directory
## <localappdata>/plugins/<ideversion>
##    where <localappdata> is e.g.
##    "%LOCALAPPDATA%\QtProject\qtcreator" on Windows Vista and later
##    "$XDG_DATA_HOME/data/QtProject/qtcreator" or "~/.local/share/data/QtProject/qtcreator" on Linux
##    "~/Library/Application Support/QtProject/Qt Creator" on OS X
#USE_USER_DESTDIR = yes #必须注释掉这句话,否则插件会生成在默认的路径,即C:\Users\firecat\AppData\Local\QtProject\QtCreator\plugins\4.8.2
###### If the plugin can be depended upon by other plugins, this code needs to be outsourced to
###### <dirname>_dependencies.pri, where <dirname> is the name of the directory containing the
###### plugin's sources.
QTC_PLUGIN_NAME = firecat_Toolbar
QTC_LIB_DEPENDS += \
    # nothing here at this time
QTC_PLUGIN_DEPENDS += \
    coreplugin
QTC_PLUGIN_RECOMMENDS += \
    # optional plugin dependencies. nothing here at this time
###### End _dependencies.pri contents ######
include($$IDE_SOURCE_TREE/src/qtcreatorplugin.pri)
RESOURCES += \
    res.qrc


注意:路径指向一定要搞正确,否则编译失败


1、IDE_SOURCE_TREE指的是Qt Creator的源码路径


2、IDE_BUILD_TREE指的是插件生成的路径,必须明确,否则会跑到默认路径:


C:\Users\<用户名>\AppData\Local\QtProject\QtCreator\plugins\4.8.2


3、include($$IDE_SOURCE_TREE/src/qtcreatorplugin.pri)


4、插件编译请选择release,因为debug没有意义,没有用处。



四、编写插件代码,我的目的是想把自己常用的功能加入到左边的工具栏中:


第一个新增按钮是计算器,点击之后会调用微软计算器;


第二个新增按钮是忽略部署直接运行;


第三个新增按钮是忽略部署直接开始调试。

image.png

bool firecat_ToolbarPlugin::initialize(const QStringList &arguments, QString *errorString)
{
    // Register objects in the plugin manager's object pool
    // Load settings
    // Add actions to menus
    // Connect to other plugins' signals
    // In the initialize function, a plugin can be sure that the plugins it
    // depends on have initialized their members.
    Q_UNUSED(arguments)
    Q_UNUSED(errorString)
    //我们把RunWithoutDeploy这个方法提携到工具栏,方便使用
    const char id1[] = "ProjectExplorer.RunWithoutDeploy";//这个id对应的是Qt Creator源码的const char RUNWITHOUTDEPLOY[]
    QAction *act1 = Core::ActionManager::command(id1)->action();//对应Qt Creator源码的m_runWithoutDeployAction
    if (act1 == NULL)
    {
        return false;
    }
    const Utils::Icon CLASSIC1(":/image/mode_run.png");//32位图片,34*34像素
    const Utils::Icon FLAT1({{":/image/mode_run_mask.png", Utils::Theme::IconsRunToolBarColor}});//8位图片,34*34像素
    const Utils::Icon FLAT_ACTIVE1({{":/image/mode_run_mask.png", Utils::Theme::IconsModeWelcomeActiveColor}});
    act1->setIcon(Utils::Icon::modeIcon(CLASSIC1, FLAT1, FLAT_ACTIVE1));
    //act1->setIcon(Utils::Icon::sideBarIcon(CLASSIC1, FLAT1));
    act1->setVisible(true);
    Core::ModeManager::addAction(act1, 130);
    //我们把DebugWithoutDeploy这个方法提携到工具栏,方便使用
    const char id2[] = "Debugger.DebugWithoutDeploy";
    QAction *act2 = Core::ActionManager::command(id2)->action();//对应Qt Creator源码的m_debugWithoutDeployAction
    if (act2 == NULL)
    {
        return false;
    }
    const Utils::Icon CLASSIC2(":/image/mode_debug.png");
    const Utils::Icon FLAT2({{":/image/mode_debug_mask.png", Utils::Theme::IconsRunToolBarColor}});
    const Utils::Icon FLAT_ACTIVE2({{":/image/mode_debug_mask.png", Utils::Theme::IconsModeWelcomeActiveColor}});
    act2->setIcon(Utils::Icon::modeIcon(CLASSIC2, FLAT2, FLAT_ACTIVE2));
    //act2->setIcon(Utils::Icon::sideBarIcon(CLASSIC2, FLAT2));
    act2->setVisible(true);
    Core::ModeManager::addAction(act2, 120);
    //我们把微软计算器提携到工具栏,方便使用
#if defined(Q_OS_WIN32)
    QAction *act3 = new QAction(tr("calc"), this);
    const Utils::Icon CLASSIC3(":/image/mode_calc.png");
    const Utils::Icon FLAT3({{":/image/mode_calc_mask.png", Utils::Theme::IconsRunToolBarColor}});
    const Utils::Icon FLAT_ACTIVE3({{":/image/mode_calc_mask.png", Utils::Theme::IconsModeWelcomeActiveColor}});
    act3->setIcon(Utils::Icon::modeIcon(CLASSIC3, FLAT3, FLAT_ACTIVE3));
    //act3->setIcon(Utils::Icon::sideBarIcon(CLASSIC3, FLAT3));
    act3->setVisible(true);
    //QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);//Qt自身没有提供System32的路径
    wchar_t szPath[MAX_PATH] ={0};
    GetSystemDirectory(szPath, MAX_PATH);
    QString path = QString::fromWCharArray(szPath);
    connect(act3, &QAction::triggered, this, [=]() {
        QProcess *poc = new QProcess;
        poc->start(path + "\\calc.exe");//即"C:\\Windows\\system32\\calc.exe"
    });
    Core::ModeManager::addAction(act3, 150);
#endif
    // 因为Qt Creator源码有定义位置摆放的优先级
    // Action priorities
    //const int  P_ACTION_RUN            = 100;
    //const int  P_ACTION_BUILDPROJECT   = 80;
    //const int  P_ACTION_DEBUG          = 90; // Priority for the modemanager.
    //ModeManager::addAction(cmd->action(), Constants::P_ACTION_RUN);
    return true;
}

                                                       



release编译生成firecat_Toolbar4.dll,然后放入到官方Qt Creator的安装路径即可。


D:\Qt\Qt5.9.8\Tools\QtCreator\lib\qtcreator\plugins\


注意,Qt官方发布的Windows版本Qt Creator IDE就是使用MSVC2015 32bit编译出来的。

image.png



大功告成(#^.^#)


image.png



五、完整的工程源码及库文件下载链接:


https://download.csdn.net/download/libaineu2004/11131466


上传的源码有一处需要调整一下,源文件夹有firecat_Toolbar.json.in文件,把它修改为:


{
    \"Name\" : \"firecat_Toolbar\",
    \"Version\" : \"$$QTCREATOR_VERSION$$VERSION_SUFFIX\",
    \"CompatVersion\" : \"$$QTCREATOR_COMPAT_VERSION\",
    \"Vendor\" : \"firecatStudio\",
    \"Copyright\" : \"firecatStudio\",
    \"License\" : \"\",
    \"Description\" : \"\",
    \"Url\" : \"\",
    $$dependencyList
}

这样可以自适应QtCreator版本号


image.png



六、如果想知道Qt Creator插件的工作原理,请参见我的下一篇博客:


Qt Creator plugin动手实践(3)C++ 类ModeManager源码分析



---


ico图标下载


https://icons8.com/


https://www.easyicon.net/


相关文章
(8)Qt中的自定义信号
本文介绍了如何在Qt框架中创建和使用自定义信号,并通过一个父子窗口切换的示例来展示自定义信号的实现和应用。
558 3
(8)Qt中的自定义信号
(7)Qt中的自定义槽(函数)
这篇文章介绍了在Qt中如何定义和使用自定义槽函数,包括类成员函数、静态类成员函数、全局函数和lambda表达式作为槽函数的示例,以及使用lambda表达式时的注意事项。
708 2
(7)Qt中的自定义槽(函数)
|
编解码 开发工具 UED
QT Widgets模块源码解析与实践
【9月更文挑战第20天】Qt Widgets 模块是 Qt 开发中至关重要的部分,提供了丰富的 GUI 组件,如按钮、文本框等,并支持布局管理、事件处理和窗口管理。这些组件基于信号与槽机制,实现灵活交互。通过对源码的解析及实践应用,可深入了解其类结构、布局管理和事件处理机制,掌握创建复杂 UI 界面的方法,提升开发效率和用户体验。
633 13
|
计算机视觉
基于QT的opencv插件框架qtCvFrameLearn实战
这篇文章详细介绍了如何基于Qt框架开发一个名为qtCvFrameLearn的OpenCV插件,包括项目配置、插件加载、Qt与OpenCV图像转换,以及通过各个插件学习OpenCV函数的使用,如仿射变换、卡通效果、腐蚀、旋转和锐化等。
395 10
|
C语言 C++ Windows
QT多插件通信框架CTK编译记录
本文记录了编译QT多插件通信框架CTK的过程,包括编译结果截图、部署配置、Log4Qt编译配置、参考链接和拓展资料。文中提供了详细的编译步骤和配置文件示例,以及相关的资源链接。
773 0
QT多插件通信框架CTK编译记录
|
IDE 开发工具 C++
qt creator + vs2019编译记录
本文记录了作者在使用qt creator和vs2019编译项目时遇到的困难和解决方案,包括编译环境设置、qt creator编译脚本的成功案例、不带Ninja的编译脚本问题、错误示范以及相关参考链接。
824 0
qt creator + vs2019编译记录
|
搜索推荐 C++
【Qt 学习笔记】Qt窗口 | 对话框 | 创建自定义对话框
【Qt 学习笔记】Qt窗口 | 对话框 | 创建自定义对话框
917 4
|
UED
【Qt 学习笔记】Qt窗口 | 工具栏 | QToolBar的使用及说明
【Qt 学习笔记】Qt窗口 | 工具栏 | QToolBar的使用及说明
2794 2
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Radio Button的使用及说明
【Qt 学习笔记】Qt常用控件 | 按钮类控件 | Radio Button的使用及说明
2677 1
|
C++ Windows
vs2019 This application failed to start because it could not find or load the QT platform plugin
这篇文章介绍了在VS2019中解决QT程序运行时出现的“无法找到或加载QT平台插件”错误的步骤,通过将必要的DLL文件和插件目录复制到项目解决方案中解决了问题。