qt_add_ui

Adds .ui files to a target.

命令的定义在 Widgets 组件对于 Qt6 包。加载包采用:

find_package(Qt6 REQUIRED COMPONENTS Widgets)
					

概要

qt_add_ui(<TARGET>
          SOURCES file1.ui [file2.ui ...]
          [INCLUDE_PREFIX <PREFIX>]
          [OPTIONS ...])
					

无版本命令 被禁用,使用 qt6_add_ui() 代替。它支持如此命令的一组相同自变量。

This command was introduced in Qt 6.8.

描述

Creates rules for calling the uic (用户界面编译器) .ui files. For each input file, a header file is generated in the build directory. The generated header files are added to sources of the target.

自变量

TARGET

The TARGET argument specifies the CMake target to which the generated header files will be added.

SOURCES

The SOURCES argument specifies the list of .ui files to process.

INCLUDE_PREFIX

INCLUDE_PREFIX specifies the include prefix for the generated header files. Use the same include prefix as in the #include directive in the source files. If ui_<basename>.h is included without a prefix, then this argument can be omitted.

选项

可以设置额外 OPTIONS 应该被添加到 uic 调用。可以查找可能的选项在 uic documentation .

范例

Without INCLUDE_PREFIX

In the following snippet, the mainwindow.cpp file includes ui_mainwindow.h and mainwindow.h .

#include "mainwindow.h"
#include "ui_mainwindow.h"
					

CMakeLists.txt is implemented as follows and it calls qt_add_ui to add ui_mainwindow.hmyapp 目标。

qt_add_executable(myapp mainwindow.cpp main.cpp)
qt_add_ui(myapp SOURCES mainwindow.ui)
					

In the above example, ui_mainwindow.h is included without a prefix. So the INCLUDE_PREFIX argument is not specified.

With INCLUDE_PREFIX

#include "mainwindow.h"
#include "src/files/ui_mainwindow.h"
					

在以上片段中, mainwindow.cpp 包括 ui_mainwindow.h with a prefix.

qt_add_executable(myapp mainwindow.cpp main.cpp)
qt_add_ui(myapp INCLUDE_PREFIX "src/files" SOURCES mainwindow.ui)
					

由于 ui_mainwindow.h is included with a prefix, the INCLUDE_PREFIX argument is specified as src/files in the above example.

Multiple .ui files

In the following snippets, both widget1.cpp and widget2.cpp 包括 ui_widget1.h and ui_widget2.h 分别。

widget1.cpp :

#include "src/files/ui_widget1.h"
					

widget2.cpp :

#include "src/files/ui_widget2.h"
					

Both ui_widget1.h and ui_widget2.h are included with the same prefix

qt_add_executable(myapp widget1.cpp widget2.cpp main.cpp)
qt_add_ui(myapp INCLUDE_PREFIX "src/files" SOURCES widget1.ui widget2.ui)
					

在此情况下, INCLUDE_PREFIX argument can be specified as src/files for both files in the above snippet.

When to prefer qt_add_ui over AUTOUIC ?

qt_add_ui has some advantages over AUTOUIC :

  • qt_add_ui ensures that the .ui files are generated correctly during the first build, for the Ninja and Ninja Multi-Config generators.
  • qt_add_ui guarantees that the generated .h files are not leaked outside the build directory.
  • 由于 qt_add_ui does not scan source files, it provides a faster build than AUTOUIC .

When to prefer qt_add_ui over qt_wrap_ui ?

qt_add_ui 拥有 INCLUDE_PREFIX argument, which can be used to specify the include prefix for the generated header files.

注意: It is not recommended to use both qt_add_ui and AUTOUIC for the same target.

另请参阅 qt_wrap_ui .