此教程基于:
- 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 文件中添加:
# qt_add_resources(<target> <resource_name> [prefix <path>] [files ...]) qt_add_resources(helloworld imageresources prefix "/" files images/logo.png )
其中, files
参数指定要添加的文件
- 点击 build 按钮, 完成构建. 此时在工程视图中可以看到logo.png已添加到工程的资源文件中.
- 修改 mainwindow.cpp, 在其中指定窗体图标.
mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent) , ui(new ui::mainwindow) { ui->setupui(this); setwindowicon(qicon(":/images/logo.png")); // todo: }
- 再次构建, 并运行. 此时窗口图标已更换为我们指定的图标文件.
软件部署(deploy)
构建生成的 helloworld.exe 在目标计算机上运行, 还需要一系列依赖的动态库. qt 提供了 qt_generate_deploy_app_script()
命令, 可以方便的打包应用程序所需的运行环境.
- 在 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)
- 切换到工程模式, 添加构建步骤, 设置构建目标为install:
- 点击build 按钮, 完成构建, 在编译输出窗口可以看到如下信息:
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 构建教程的方法步骤的文章就介绍到这了,更多相关qt creator cmake 构建内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论