Qt Creator + CMake 構(gòu)建教程的方法步驟
此教程基于:
- Qt 6.7.4
- Qt Creator 15.0.1
- CMake 3.26.4
Qt 6 以下的版本使用 CMake 構(gòu)建可能會(huì)存在一些問題.
此教程描述了如何一步步在 Qt Creator 中使用 CMake 構(gòu)建應(yīng)用程序工程. 涉及 新建窗體工程, 更新翻譯, 添加資源, 以及軟件部署等環(huán)節(jié).
新建窗體工程
此過程描述如何在Qt Creator中新建一個(gè)使用 CMake 構(gòu)建的窗體應(yīng)用程序.
- 運(yùn)行
Qt Creator, 點(diǎn)擊Welcome頁中的Create Project...按鈕. - 在新建工程對(duì)話框中選擇: Application(Qt) | Qt Widgets Application, 點(diǎn)擊右下角 Choose… 按鈕.

- 設(shè)置新建工程的名稱和路徑, 點(diǎn)擊 Next.

- 構(gòu)建系統(tǒng)選擇: CMake, 點(diǎn)擊 Next.

- 類信息頁不做修改, 點(diǎn)擊 Next.

- 翻譯文件頁, 選擇: Chinese(China), 點(diǎn)擊 Next.


- 點(diǎn)擊 Finish 按鈕, 完成新建工程.

- 新建工程完畢后, 開發(fā)界面如下圖所示. 工程的構(gòu)建, 調(diào)試, 運(yùn)行, 以及編譯套件和編譯配置的切換分別對(duì)應(yīng)圖中的1,2,3,4.

- 這里我們選擇 Desktop_Qt_6_7_3_MSVC2019_64bit.

- 點(diǎn)擊 構(gòu)建 按鈕, 完成工程的編譯; 點(diǎn)擊 運(yùn)行 按鈕, 運(yùn)行示例程序.
更新翻譯
- 使用 Linguist 打開 helloworld_zh_CN.ts(建議將 .ts 文件的打開方式直接設(shè)置為 Linguist)

- 設(shè)置 MainWindow 的中文翻譯為: 主窗體, 確認(rèn)并保存.

- 注釋掉:
qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES}), 并添加:qt_add_translations(TARGETS helloworld TS_FILES ${TS_FILES})(helloworld 是此工程的構(gòu)建目標(biāo)), 然后保存.

- 切換到Projects 模式頁, 點(diǎn)擊 Add Build Step 工具按鈕, 選擇 CMake Build 菜單.

- 勾選 update_translations目標(biāo), 去除勾選 all目標(biāo), 并將此構(gòu)建步驟向上移動(dòng) (或直接修改構(gòu)建步驟中的第一個(gè))

- 點(diǎn)擊 Build 按鈕, 重新構(gòu)建. 從編譯輸出窗口中, 可以看到程序的構(gòu)建過程為:
更新 helloworld_zh_CN.ts;
生成 helloworld_zh_CN.qm;
鏈接生成 helloworld.exe.

- 點(diǎn)擊 Run按鈕運(yùn)行此程序, 可以看到主窗體的標(biāo)題欄已經(jīng)顯示為中文.

添加資源
此過程, 我們將添加一個(gè)圖標(biāo)資源, 并將此圖標(biāo)設(shè)置為主窗體的窗口圖標(biāo).
- 使用資源管理器打開工程所在目錄, 新建名為 images 的文件夾, 并將圖標(biāo)
放置到此文件夾下. - 在 CMakeLists.txt 文件中添加:
# qt_add_resources(<TARGET> <RESOURCE_NAME> [PREFIX <PATH>] [FILES ...])
qt_add_resources(helloworld imageresources
PREFIX "/"
FILES images/logo.png
)
其中, FILES參數(shù)指定要添加的文件
- 點(diǎn)擊 Build 按鈕, 完成構(gòu)建. 此時(shí)在工程視圖中可以看到logo.png已添加到工程的資源文件中.

- 修改 MainWindow.cpp, 在其中指定窗體圖標(biāo).
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowIcon(QIcon(":/images/logo.png")); // TODO:
}
- 再次構(gòu)建, 并運(yùn)行. 此時(shí)窗口圖標(biāo)已更換為我們指定的圖標(biāo)文件.

軟件部署(Deploy)
構(gòu)建生成的 helloworld.exe 在目標(biāo)計(jì)算機(jī)上運(yùn)行, 還需要一系列依賴的動(dòng)態(tài)庫. Qt 提供了 qt_generate_deploy_app_script() 命令, 可以方便的打包應(yīng)用程序所需的運(yùn)行環(huán)境.
- 在 CMakeLists.txt 中添加以下代碼, 生成部署腳本:
qt_generate_deploy_app_script(
TARGET helloworld
OUTPUT_SCRIPT deploy_script
NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})
- 在 CMakeLists.txt 中設(shè)置安裝目錄前綴 (
CMAKE_INSTALL_PREFIX)為${PROJECT_BINARY_DIR}/install:
set(CMAKE_INSTALL_PREFIX ${PROJECT_BINARY_DIR}/install)
- 切換到工程模式, 添加構(gòu)建步驟, 設(shè)置構(gòu)建目標(biāo)為install:

- 點(diǎn)擊Build 按鈕, 完成構(gòu)建, 在編譯輸出窗口可以看到如下信息:
12:36:02: Starting: "C:\Program Files\CMake\bin\cmake.exe" --build D:/workspace/qt/helloworld/build/Desktop_Qt_6_7_3_MSVC2019_64bit-Debug --target install [0/1 ?/sec] Install the project... -- Install configuration: "Debug" -- Writing D:/workspace/qt/helloworld/build/Desktop_Qt_6_7_3_MSVC2019_64bit-Debug/install/bin/qt.conf -- Running Qt deploy tool for D:/workspace/qt/helloworld/build/Desktop_Qt_6_7_3_MSVC2019_64bit-Debug/helloworld.exe in working directory 'D:/workspace/qt/helloworld/build/Desktop_Qt_6_7_3_MSVC2019_64bit-Debug/install' 'C:/Qt/6.7.3/msvc2019_64/bin/windeployqt.exe' 'D:/workspace/qt/helloworld/build/Desktop_Qt_6_7_3_MSVC2019_64bit-Debug/helloworld.exe' '--dir' '.' '--libdir' 'bin' '--plugindir' 'plugins' '--qml-deploy-dir' 'qml' '--translationdir' 'translations' '--force' '--qtpaths' 'C:/Qt/6.7.3/msvc2019_64/bin/qtpaths6.exe' D:\workspace\qt\helloworld\build\Desktop_Qt_6_7_3_MSVC2019_64bit-Debug\helloworld.exe 64 bit, debug executable Adding in plugin type generic for module: Qt6Gui Skipping plugin qinsighttrackerd.dll. Use -deploy-insighttracker if you want to use it. Adding Qt6Network for qtuiotouchplugind.dll from plugin type: generic Adding in plugin type iconengines for module: Qt6Gui Adding Qt6Svg for qsvgicond.dll from plugin type: iconengines Adding in plugin type imageformats for module: Qt6Gui Adding Qt6Pdf for qpdfd.dll from plugin type: imageformats Adding in plugin type networkinformation for module: Qt6Network Adding in plugin type platforminputcontexts for module: Qt6Gui Skipping plugin qtvirtualkeyboardplugind.dll due to disabled dependencies (Qt6Qml Qt6Quick). Adding in plugin type platforms for module: Qt6Gui Adding in plugin type styles for module: Qt6Widgets Adding in plugin type tls for module: Qt6Network Direct dependencies: Qt6Core Qt6Gui Qt6Widgets All dependencies : Qt6Core Qt6Gui Qt6Widgets To be deployed : Qt6Core Qt6Gui Qt6Network Qt6Pdf Qt6Svg Qt6Widgets Updating Qt6Cored.dll. Updating Qt6Guid.dll. ......... Creating qt_zh_CN.qm... Creating qt_zh_TW.qm... -- Installing: D:/workspace/qt/helloworld/build/Desktop_Qt_6_7_3_MSVC2019_64bit-Debug/install/bin/helloworld.exe 12:36:03: The process "C:\Program Files\CMake\bin\cmake.exe" exited normally. 12:36:03: Elapsed time: 00:03.
在資源管理器中打開 D:/workspace/qt/helloworld/build/Desktop_Qt_6_7_3_MSVC2019_64bit-Debug/install/bin目錄, 運(yùn)行 hello world.exe, 此時(shí)程序可以正常運(yùn)行.
到此這篇關(guān)于Qt Creator + CMake 構(gòu)建教程的方法步驟的文章就介紹到這了,更多相關(guān)Qt Creator CMake 構(gòu)建內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- QT Creator配置Kit的實(shí)現(xiàn)示例
- C++使用QTcreator創(chuàng)建動(dòng)態(tài)庫流程
- Qt?QtCreator添加自定義注釋的實(shí)現(xiàn)方法
- QT Creator+OpenCV實(shí)現(xiàn)圖像灰度化的示例代碼
- Qt?Creator配置opencv環(huán)境的全過程記錄
- Qt creator中項(xiàng)目的構(gòu)建配置和運(yùn)行設(shè)置的步驟
- Qt Creator使用教程的簡單說明
- VS2012下QT creator登錄對(duì)話框設(shè)計(jì)
- 新版本Qt Creator安裝配置的實(shí)現(xiàn)步驟
相關(guān)文章
C++中?‘=default?’及‘?=delete?’的使用
這篇文章主要介紹了C++中?=default?及?=delete?使用,使用=default和=delete可以控制編譯器默認(rèn)函數(shù)體的使用,下面我們就來看看具體的室友方法吧,需要的朋友也可以參考一下2021-12-12
浮點(diǎn)數(shù)乘法和整形乘除法的效率經(jīng)驗(yàn)比較
這篇文章主要為大家介紹了浮點(diǎn)數(shù)乘法和整形乘除法的效率經(jīng)驗(yàn)比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
C++實(shí)現(xiàn)動(dòng)態(tài)綁定代碼分享
對(duì)于C++動(dòng)態(tài)綁定的理解,就是編譯器用靜態(tài)分析的方法加上虛擬函數(shù)的設(shè)計(jì)實(shí)現(xiàn)在程序運(yùn)行時(shí)動(dòng)態(tài)智能執(zhí)行正確虛擬函數(shù)的技術(shù)。要徹底理解動(dòng)態(tài)綁定,只需要掌握兩點(diǎn),一是編譯器的靜態(tài)編譯過程,二是虛擬函數(shù)的基本知識(shí)。只要有了這兩點(diǎn)理解,任何動(dòng)態(tài)綁定的分析都是很容易的2015-11-11

