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 .