Qt Creator + CMake 構(gòu)建教程的方法步驟
此教程基于:
- Qt 6.7.4
- Qt Creator 15.0.1
- CMake 3.26.4
Qt 6 以下的版本使用 CMake 構(gòu)建可能會存在一些問題.
此教程描述了如何一步步在 Qt Creator 中使用 CMake 構(gòu)建應用程序工程. 涉及 新建窗體工程, 更新翻譯, 添加資源, 以及軟件部署等環(huán)節(jié).
新建窗體工程
此過程描述如何在Qt Creator中新建一個使用 CMake 構(gòu)建的窗體應用程序.
- 運行
Qt Creator
, 點擊Welcome
頁中的Create Project...
按鈕. - 在新建工程對話框中選擇: Application(Qt) | Qt Widgets Application, 點擊右下角 Choose… 按鈕.
- 設置新建工程的名稱和路徑, 點擊 Next.
- 構(gòu)建系統(tǒng)選擇: CMake, 點擊 Next.
- 類信息頁不做修改, 點擊 Next.
- 翻譯文件頁, 選擇: Chinese(China), 點擊 Next.
- 點擊 Finish 按鈕, 完成新建工程.
- 新建工程完畢后, 開發(fā)界面如下圖所示. 工程的構(gòu)建, 調(diào)試, 運行, 以及編譯套件和編譯配置的切換分別對應圖中的1,2,3,4.
- 這里我們選擇 Desktop_Qt_6_7_3_MSVC2019_64bit.
- 點擊 構(gòu)建 按鈕, 完成工程的編譯; 點擊 運行 按鈕, 運行示例程序.
更新翻譯
- 使用 Linguist 打開 helloworld_zh_CN.ts(建議將 .ts 文件的打開方式直接設置為 Linguist)
- 設置 MainWindow 的中文翻譯為: 主窗體, 確認并保存.
- 注釋掉:
qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
, 并添加:qt_add_translations(TARGETS helloworld TS_FILES ${TS_FILES})
(helloworld 是此工程的構(gòu)建目標), 然后保存.
- 切換到Projects 模式頁, 點擊 Add Build Step 工具按鈕, 選擇 CMake Build 菜單.
- 勾選 update_translations目標, 去除勾選 all目標, 并將此構(gòu)建步驟向上移動 (或直接修改構(gòu)建步驟中的第一個)
- 點擊 Build 按鈕, 重新構(gòu)建. 從編譯輸出窗口中, 可以看到程序的構(gòu)建過程為:
更新 helloworld_zh_CN.ts;
生成 helloworld_zh_CN.qm;
鏈接生成 helloworld.exe.
- 點擊 Run按鈕運行此程序, 可以看到主窗體的標題欄已經(jīng)顯示為中文.
添加資源
此過程, 我們將添加一個圖標資源, 并將此圖標設置為主窗體的窗口圖標.
- 使用資源管理器打開工程所在目錄, 新建名為 images 的文件夾, 并將圖標
放置到此文件夾下.
- 在 CMakeLists.txt 文件中添加:
# qt_add_resources(<TARGET> <RESOURCE_NAME> [PREFIX <PATH>] [FILES ...]) qt_add_resources(helloworld imageresources PREFIX "/" FILES images/logo.png )
其中, FILES
參數(shù)指定要添加的文件
- 點擊 Build 按鈕, 完成構(gòu)建. 此時在工程視圖中可以看到logo.png已添加到工程的資源文件中.
- 修改 MainWindow.cpp, 在其中指定窗體圖標.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setWindowIcon(QIcon(":/images/logo.png")); // TODO: }
- 再次構(gòu)建, 并運行. 此時窗口圖標已更換為我們指定的圖標文件.
軟件部署(Deploy)
構(gòu)建生成的 helloworld.exe 在目標計算機上運行, 還需要一系列依賴的動態(tài)庫. Qt 提供了 qt_generate_deploy_app_script()
命令, 可以方便的打包應用程序所需的運行環(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 中設置安裝目錄前綴 (
CMAKE_INSTALL_PREFIX
)為${PROJECT_BINARY_DIR}/install
:
set(CMAKE_INSTALL_PREFIX ${PROJECT_BINARY_DIR}/install)
- 切換到工程模式, 添加構(gòu)建步驟, 設置構(gòu)建目標為install:
- 點擊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目錄, 運行 hello world.exe, 此時程序可以正常運行.
到此這篇關于Qt Creator + CMake 構(gòu)建教程的方法步驟的文章就介紹到這了,更多相關Qt Creator CMake 構(gòu)建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
static_cast,dynamic_cast,reinterpret_cast,const_cast的區(qū)別及用法詳解
以下是對static_cast,dynamic_cast,reinterpret_cast,const_cast的區(qū)別及用法進行了詳細的分析介紹,需要的朋友可以過來參考下2013-09-09