c++ – Can't figure out how qt_wrap_ui (cmake qt) works


I use clion and cmake accordingly. I needed .ui files (MainWindow.ui). And I understand that to use them, you need to register the qt_wrap_ui command in CMakeLists.
The documentation says that it should be used like this:

qt_wrap_ui(resultingLibraryName HeadersDestName SourcesDestName SourceLists ...)

Can't figure out what the HeadersDestName and SourcesDestName parameters mean.


The qt_wrap_ui function qt_wrap_ui very ancient. And I have never seen it in the QT CMake documentation. And apparently it only works with Qt3.

The answer to the question "What do the HeadersDestName and SourcesDestName parameters mean?"

These are variables into which the paths to the generated '.h' and '.cxx' files, respectively, will be added. They use something like this:

qt_wrap_ui(mylib ui_headers ui_sources ${ui_source_files})
add_executable(myapp ${ui_headers} ${ui_sources} ${main_sources})

Its name is very similar to two slightly newer functions: qt4_wrap_ui and qt5_wrap_ui which are part of Qt4 and Qt5 respectively. Note that these are not CMake functions qt_wrap_ui .

The answer to the potential question "what to use instead for Qt5?"

There are now two main ways to build a Qt5 project using CMake:

CMake approach

It is enough to enable set(CMAKE_AUTOUIC ON) and CMake will automatically find all the files for which you need to generate ui_*.h files and add the necessary compilation steps.

QT approach

If for some reason it is not possible to use AUTOUIC , then you can take the qt5_wrap_ui function. But here you need to add a couple of steps.

  1. Find all .ui files
  2. Call the function `qt5_wrap_ui (ui_files" <all .ui files> ")
  3. Add generated ui_files to the list of all other code files in this project add_library(mylib "<исходные файлы проекта>" ${ui_files})
Scroll to Top