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使用教程的簡(jiǎn)單說明
- VS2012下QT creator登錄對(duì)話框設(shè)計(jì)
- 新版本Qt Creator安裝配置的實(shí)現(xiàn)步驟
相關(guān)文章
使用Qt開發(fā)實(shí)現(xiàn)字幕滾動(dòng)效果
我們經(jīng)常能夠在外面看到那種滾動(dòng)字幕,那么就拿qt來做一個(gè)吧,文章通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11深入理解:Java是類型安全的語言,而C++是非類型安全的語言
本篇文章是對(duì)Java是類型安全的語言,而C++是非類型安全的語言進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06如何用C寫一個(gè)web服務(wù)器之I/O多路復(fù)用
本文主要介紹了如何用C寫一個(gè)web服務(wù)器之I/O多路復(fù)用,本次選擇了 I/O 模型的優(yōu)化,因?yàn)樗欠?wù)器的基礎(chǔ),這個(gè)先完成的話,后面的優(yōu)化就可以選擇各個(gè)模塊來進(jìn)行,不必進(jìn)行全局化的改動(dòng)了。2021-05-05C++文件的數(shù)據(jù)寫入和文件的數(shù)據(jù)讀取的方法實(shí)現(xiàn)
本文主要介紹了C++文件的數(shù)據(jù)寫入和文件的數(shù)據(jù)讀取的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06C語言實(shí)現(xiàn)基于控制臺(tái)的電子時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)基于控制臺(tái)的電子時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05