it编程 > 编程语言 > C/C++

Qt Creator + CMake 构建教程的方法步骤

16人参与 2025-02-27 C/C++

此教程基于:

qt 6 以下的版本使用 cmake 构建可能会存在一些问题.

此教程描述了如何一步步在 qt creator 中使用 cmake 构建应用程序工程. 涉及 新建窗体工程, 更新翻译, 添加资源, 以及软件部署等环节.

新建窗体工程

此过程描述如何在qt creator中新建一个使用 cmake 构建的窗体应用程序.

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

更新翻译

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

- 勾选 update_translations目标, 去除勾选 all目标, 并将此构建步骤向上移动 (或直接修改构建步骤中的第一个)

在这里插入图片描述

在这里插入图片描述

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

在这里插入图片描述

添加资源

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

 # qt_add_resources(<target> <resource_name> [prefix <path>] [files ...])
    qt_add_resources(helloworld imageresources
        prefix "/"
        files images/logo.png
    )

其中, files参数指定要添加的文件

在这里插入图片描述

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() 命令, 可以方便的打包应用程序所需的运行环境.

qt_generate_deploy_app_script(
    target helloworld
    output_script deploy_script
    no_unsupported_platform_error
)
install(script ${deploy_script})
set(cmake_install_prefix ${project_binary_dir}/install)

在这里插入图片描述

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 构建内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网! 

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

多线程-lock与lockInterruptibly的区别及说明

02-27

C++中pair使用的示例代码

02-26

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

02-28

C++中BitSet和Bloom_Filter的实现

02-28

深入理解Qt 初始项目代码

02-28

Qt 中集成mqtt协议的使用方法

02-24

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论