Qt Creator + CMake 构建教程

打印 上一主题 下一主题

主题 907|帖子 907|积分 2721

此教程基于:
  

  • Qt 6.7.4
  • Qt Creator 15.0.1
  • CMake 3.26.4
  Qt 6 以下的版本利用 CMake 构建可能会存在一些题目.
  
  
此教程形貌了如何一步步在 Qt Creator 中利用 CMake 构建应用程序工程. 涉及 新建窗体工程, 更新翻译, 添加资源, 以及软件部署等环节.
新建窗体工程

此过程形貌如何在Qt Creator中新建一个利用 CMake 构建的窗体应用程序.


  • 运行 Qt Creator, 点击Welcome页中的 Create Project... 按钮.
  • 在新建工程对话框中选择: Application(Qt) | Qt Widgets Application, 点击右下角 Choose… 按钮.



  • 设置新建工程的名称和路径, 点击 Next.



  • 构建系统选择: CMake, 点击 Next.



  • 类信息页不做修改, 点击 Next.



  • 翻译文件页, 选择: Chinese(China), 点击 Next.







  • 点击 Finish 按钮, 完成新建工程.



  • 新建工程完毕后, 开发界面如下图所示. 工程的构建, 调试, 运行, 以及编译套件和编译配置的切换分别对应图中的1,2,3,4.



  • 这里我们选择 Desktop_Qt_6_7_3_MSVC2019_64bit.



  • 点击 构建 按钮, 完成工程的编译; 点击 运行 按钮, 运行示例程序.
更新翻译



  • 利用 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 是此工程的构建目的), 然后生存.



  • 切换到Projects 模式页, 点击 Add Build Step 工具按钮, 选择 CMake Build 菜单.
- 勾选 update_translations目的, 去除勾选 all目的, 并将此构建步骤向上移动 (或直接修改构建步骤中的第一个)



  • 点击 Build 按钮, 重新构建. 从编译输出窗口中, 可以看到程序的构建过程为:

    • 更新 helloworld_zh_CN.ts;
    • 生成 helloworld_zh_CN.qm;
    • 链接生成 helloworld.exe.

- 点击 Run按钮运行此程序, 可以看到主窗体的标题栏已经显示为中文.

添加资源

此过程, 我们将添加一个图标资源, 并将此图标设置为主窗体的窗口图标.


  • 利用资源管理器打开工程所在目录, 新建名为 images 的文件夹, 并将图标
    放置到此文件夹下.
  • CMakeLists.txt 文件中添加:
  1. # qt_add_resources(<TARGET> <RESOURCE_NAME> [PREFIX <PATH>] [FILES ...])
  2.     qt_add_resources(helloworld imageresources
  3.         PREFIX "/"
  4.         FILES images/logo.png
  5.     )
复制代码
此中, FILES参数指定要添加的文件


  • 点击 Build 按钮, 完成构建. 此时在工程视图中可以看到logo.png已添加到工程的资源文件中.



  • 修改 MainWindow.cpp, 在此中指定窗体图标.
  1. MainWindow::MainWindow(QWidget *parent)
  2.     : QMainWindow(parent)
  3.     , ui(new Ui::MainWindow)
  4. {
  5.     ui->setupUi(this);
  6.     setWindowIcon(QIcon(":/images/logo.png")); // TODO:
  7. }
复制代码


  • 再次构建, 并运行. 此时窗口图标已更换为我们指定的图标文件.

软件部署(Deploy)

构建生成的 helloworld.exe 在目的盘算机上运行, 还需要一系列依赖的动态库. Qt 提供了 qt_generate_deploy_app_script() 命令, 可以方便的打包应用程序所需的运行情况.


  • CMakeLists.txt 中添加以下代码, 生成部署脚本:
  1. qt_generate_deploy_app_script(
  2.     TARGET helloworld
  3.     OUTPUT_SCRIPT deploy_script
  4.     NO_UNSUPPORTED_PLATFORM_ERROR
  5. )
  6. install(SCRIPT ${deploy_script})
复制代码


  • CMakeLists.txt 中设置安装目录前缀 (CMAKE_INSTALL_PREFIX )为 ${PROJECT_BINARY_DIR}/install:
  1. set(CMAKE_INSTALL_PREFIX ${PROJECT_BINARY_DIR}/install)
复制代码


  • 切换到工程模式, 添加构建步骤, 设置构建目的为install:



  • 点击Build 按钮, 完成构建, 在编译输出窗口可以看到如下信息:
  1. 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
  2. [0/1 ?/sec] Install the project...
  3. -- Install configuration: "Debug"
  4. -- Writing D:/workspace/qt/helloworld/build/Desktop_Qt_6_7_3_MSVC2019_64bit-Debug/install/bin/qt.conf
  5. -- 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'
  6. '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'
  7. D:\workspace\qt\helloworld\build\Desktop_Qt_6_7_3_MSVC2019_64bit-Debug\helloworld.exe 64 bit, debug executable
  8. Adding in plugin type generic for module: Qt6Gui
  9. Skipping plugin qinsighttrackerd.dll. Use -deploy-insighttracker if you want to use it.
  10. Adding Qt6Network for qtuiotouchplugind.dll from plugin type: generic
  11. Adding in plugin type iconengines for module: Qt6Gui
  12. Adding Qt6Svg for qsvgicond.dll from plugin type: iconengines
  13. Adding in plugin type imageformats for module: Qt6Gui
  14. Adding Qt6Pdf for qpdfd.dll from plugin type: imageformats
  15. Adding in plugin type networkinformation for module: Qt6Network
  16. Adding in plugin type platforminputcontexts for module: Qt6Gui
  17. Skipping plugin qtvirtualkeyboardplugind.dll due to disabled dependencies (Qt6Qml Qt6Quick).
  18. Adding in plugin type platforms for module: Qt6Gui
  19. Adding in plugin type styles for module: Qt6Widgets
  20. Adding in plugin type tls for module: Qt6Network
  21. Direct dependencies: Qt6Core Qt6Gui Qt6Widgets
  22. All dependencies   : Qt6Core Qt6Gui Qt6Widgets
  23. To be deployed     : Qt6Core Qt6Gui Qt6Network Qt6Pdf Qt6Svg Qt6Widgets
  24. Updating Qt6Cored.dll.
  25. Updating Qt6Guid.dll.
  26. .........
  27. Creating qt_zh_CN.qm...
  28. Creating qt_zh_TW.qm...
  29. -- Installing: D:/workspace/qt/helloworld/build/Desktop_Qt_6_7_3_MSVC2019_64bit-Debug/install/bin/helloworld.exe
  30. 12:36:03: The process "C:\Program Files\CMake\bin\cmake.exe" exited normally.
  31. 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, 此时程序可以正常运行.

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

自由的羽毛

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表