Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
qt_add_ui¶
Adds .ui files to a target.
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.
from mainwindow import * from ui_mainwindow import *
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¶
from mainwindow import * from src.files.ui_mainwindow import *
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:
from src.files.ui_widget1 import *
widget2.cpp:
from src.files.ui_widget2 import *
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_uiensures that the.uifiles are generated correctly during the first build, for theNinjaandNinja Multi-Configgenerators.
qt_add_uiguarantees that the generated.hfiles are not leaked outside the build directory.Since
qt_add_uidoes not scan source files, it provides a faster build thanAUTOUIC.
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