Qt6 CMake: Automatic Packaging and Deployment of QWidgets

Hi~ This is Weekly Qt, dedicated to sharing valuable Qt knowledge.This section demonstrates how to deploy a Qt Widgets application through a simple C++ Qt project example. It provides a detailed introduction to using the deployment features of Qt’s extended CMake.01 Project StructureThe basic structure of the project is as follows:

BuildWidgetAppAndDeploy/├── CMakeLists.txt├── main.cpp├── mainwindow.cpp├── mainwindow.h└── mainwindow.ui

02 CMake Deployment ConfigurationPreviously, we introduced the basic structure of a QWidget project, and here we focus on the CMake configuration related to deployment. In the past, using qmake required manually using tools like windeployqt for packaging, but now the CMake interface can automate these tasks.

First, add the deployment-related configuration in CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)project(BuildWidgetAppAndDeploy VERSION 1.0 LANGUAGES CXX)set(CMAKE_INCLUDE_CURRENT_DIR ON)set(CMAKE_AUTOUIC ON)set(CMAKE_AUTOMOC ON)set(CMAKE_AUTORCC ON)find_package(Qt6 REQUIRED COMPONENTS Widgets)# Set the installation directory, modify as neededif(WIN32)    set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install")else()    set(CMAKE_INSTALL_PREFIX "/usr/local")endif()# Add executableqt_add_executable(BuildWidgetAppAndDeploy    main.cpp    mainwindow.cpp    mainwindow.h    mainwindow.ui)# Link Qt librariestarget_link_libraries(BuildWidgetAppAndDeploy PRIVATE Qt6::Widgets)# Generate deployment scriptqt_generate_deploy_app_script(    TARGET BuildWidgetAppAndDeploy    OUTPUT_SCRIPT deploy_script    NO_UNSUPPORTED_PLATFORM_ERROR # Allow script generation on all platforms)# Configure installation rulesinstall(TARGETS BuildWidgetAppAndDeploy    BUNDLE DESTINATION .    RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)install(SCRIPT ${deploy_script})

03 Explanation of Deployment Command

The main parameters of the qt_generate_deploy_app_script() command are as follows:

TARGET: Specifies the target application to be deployed

OUTPUT_SCRIPT: The variable name for the generated deployment script

NO_UNSUPPORTED_PLATFORM_ERROR: Allows script generation on all platforms, even if some platforms may not be fully supported

04 Deployment Steps

1. Build the project:

# In the project root directorymkdir build && cd buildcmake ..cmake --build .

2. Install and deploy:

# Windows/Linuxcmake --install . --prefix "./install"# Specify installation directory as build/install# macOScmake --install . --prefix "./BuildWidgetAppAndDeploy.app"

You can also select the deployment target install on the project page in Qt Creator, and running it again will automatically deploy.

05 Structure of the Deployed Target

Refer to the effect on the Windows platform

install/├── bin/│   ├── BuildWidgetAppAndDeploy.exe│   ├── Qt6Core.dll│   ├── Qt6Gui.dll│   ├── Qt6Widgets.dll│   └── platforms/└── plugins/

06 Handling Third-Party Dependencies

If the project uses third-party libraries outside of Qt, you can implement this in CMake by manually configuring the deployment of these libraries:

# Example Windows third-party libraryif(WIN32)    install(FILES         "${THIRD_PARTY_LIB_DIR}/example.dll"        DESTINATION bin    )endif()

References

[1]https://doc.qt.io/qt-6/cmake-deployment.html

We welcome submissions to [email protected], with no formatting requirements. Your best practices deserve to be seen!

Related Articles

Qt6 CMake: Goodbye qmake, Introduction to CMake

Qt6 CMake: Building QWidgets Applications

Qt6 CMake: Managing Qt Resource Files

Qt6 CMake: Creating Qt Static and Dynamic Libraries

Qt6 CMake: Setting Up Qt Multi-Directory Projects

Qt6 CMake: Automatic Internationalization of QWidgets

Leave a Comment