- Qt 6.11
- Qt Widgets
- qt_add_ui
- Qt 6.11.0 Reference Documentation

# qt_add_ui

Adds .ui files to a target.

The command is defined in the `Widgets` component of the `Qt6` package. Load the package with:

     find_package(Qt6 REQUIRED COMPONENTS Widgets)

## Synopsis

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

If versionless commands are disabled, use `qt6_add_ui()` instead. It supports the same set of arguments as this command.

This command was introduced in Qt 6.8.

## Description

Creates rules for calling the User Interface Compiler (uic) on the `.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.

## Arguments

### 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

You can set additional `OPTIONS` that should be added to the `uic` calls. You can find possible options in the uic documentation.

## Examples

### 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.h` to the `myapp` target.

     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"

In the above snippet, `mainwindow.cpp` includes `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)

Since `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` include `ui_widget1.h` and `ui_widget2.h` respectively.

`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)

In this case, the `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.
- Since `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` has the `INCLUDE_PREFIX` argument, which can be used to specify the include prefix for the generated header files.

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

See also qt_wrap_ui.