天天看點

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
Qt Creator plugin動手實踐(2)自己動手寫qt creator插件,實作自定義工具欄按鈕
Qt Creator plugin動手實踐(2)自己動手寫qt creator插件,實作自定義工具欄按鈕
工程.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沒有意義,沒有用處。

四、編寫插件代碼,我的目的是想把自己常用的功能加入到左邊的工具欄中:

第一個新增按鈕是電腦,點選之後會調用微軟電腦;

第二個新增按鈕是忽略部署直接運作;

第三個新增按鈕是忽略部署直接開始調試。

Qt Creator plugin動手實踐(2)自己動手寫qt creator插件,實作自定義工具欄按鈕
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編譯出來的。

Qt Creator plugin動手實踐(2)自己動手寫qt creator插件,實作自定義工具欄按鈕

大功告成(#^.^#)

Qt Creator plugin動手實踐(2)自己動手寫qt creator插件,實作自定義工具欄按鈕

五、完整的工程源碼及庫檔案下載下傳連結:

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版本号

Qt Creator plugin動手實踐(2)自己動手寫qt creator插件,實作自定義工具欄按鈕

六、如果想知道Qt Creator插件的工作原理,請參見我的下一篇部落格:

Qt Creator plugin動手實踐(3)C++ 類ModeManager源碼分析

---

ico圖示下載下傳

https://icons8.com/ https://www.easyicon.net/

繼續閱讀