6

I use Qt with CMake because CMake integrates with my team's work easier than my own. I have frequently encountered an error along the lines of

ui_*.h: No such file or directory

Usually when my project already has a ui_*.h file to start with it will just modify that .h file. I do use the below commands in my CMake file, so it should be wrapping my .ui file with the appropriate ui_*.h file.

qt4_wrap_ui (mainwindow  mainwindow.ui)
target_linked_library (mainwindow ${QT_LIBRARIES})

But sometimes that doesn't work and I have to completely rebuild the entire ui_*.h file. What am I doing wrong?

Daniel Arnett
  • 493
  • 2
  • 8
  • 18

6 Answers6

13

For anyone having this problem in the future. I pretty much followed the demo here.

http://doc.qt.io/qt-5/cmake-manual.html

Adding, the following line to the CMakeLists.txt should get rid of this problem.

set(CMAKE_AUTOUIC ON)

From the CMake documentation at

https://cmake.org/cmake/help/v3.0/prop_tgt/AUTOUIC.html

AUTOUIC is a boolean specifying whether CMake will handle the Qt uic code generator automatically, i.e. without having to use the QT4_WRAP_UI() or QT5_WRAP_UI() macro. Currently Qt4 and Qt5 are supported.

One small note, this property is available in CMake versions 3.0.2+. So below that, @rbaleksandar's solution should be more appropriate.

Hope that helps.

Sami
  • 146
  • 1
  • 5
  • 2
    Setting `CMAKE_AUTOUIC` to `ON`only works if the project stores `*.ui` files in the same folder as the C++ source files. Otherwise the project fails to include the `ui_*.h` files. – RAM Apr 19 '19 at 08:31
  • You might have to set set(CMAKE_AUTOUIC ON) before add_executable(). – KungPhoo Oct 22 '19 at 14:52
2

The quick solution is to use UIC. In bash navigate to the directory containing your *.ui file and run (for the mainwindow.ui example)

uic mainwindow.ui -o ui_mainwindow.h

and then move the newly generated ui_mainwindow.h file to your build directory.

mv ui_mainwindow.h ../build_Qt_4_8_5-Debug/

You shouldn't see the 'No such file or directory' error anymore and can confidently move on to the many other wonderful errors to find in the world of Qt with CMake.

Daniel Arnett
  • 493
  • 2
  • 8
  • 18
  • I don't agree that running `uic` by hand definitely solves the problem. @Sami response is the correct one. Setting `set(CMAKE_AUTOUIC ON)` will instruct CMake to generate the `ui_*.h` files automatically when needed. – j4x May 08 '17 at 12:41
1

If I remember correctly you actually have to add your UI files to the add_executable(...) like this:

qt4_wrap_ui(UI_HEADERS mainwindow.ui ...) # Add all UI files here like you've done it
...
add_executable(${PROJECT_NAME} ${SRC} ${UI_HEADERS}) # Add them to the executable
...

After all UI files are actually converted to header and source files, which naturally have to be compiled along with the rest of your code.

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
  • Good catch, I forgot to put that in the question here, but that still doesn't do it. I have to manually run `uic` as described in my answer. – Daniel Arnett May 25 '16 at 20:54
  • Can you post your complete `CMakeLists.txt`? I have the feeling that something is missing. I no longer use UI files (I code the UI myself) but I do remember that I have never ever ran manually `uic` or moved any files whatsoever. – rbaleksandar May 25 '16 at 21:02
  • I guess @Sami answer complete this one: `set(CMAKE_AUTOUIC ON)` is required so this answer works. – Adrian Maire Apr 10 '17 at 08:49
0

Building with CMake seems to have an error in

if(CMAKE_VERSION VERSION_LESS "3.7.0")
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()

remove if in your CMakeLists.txt like this:

set(CMAKE_INCLUDE_CURRENT_DIR ON)

This works in my system with CMake 3.12.1 and Windows 10.

Ignatius
  • 2,745
  • 2
  • 20
  • 32
0

when the ui files are stored in a different location the CMAKE_AUTOUIC_SEARCH_PATHS can be set to include the custom locations so that the CMAKE_AUTOUIC option will find the ui files.

AngryDane
  • 89
  • 1
  • 4
0

None of the previous answers have helped me. I tried Ctrl + RMB on #include "./ui_mainwindow.h" in Qt Creator and after that the error did not appear since.

Few details to mention:

  • set(CMAKE_AUTOUIC ON) was enabled before add_executable()
  • cmake_minimum_required(VERSION 3.5)
  • I was using qt6
Anton Ionov
  • 21
  • 1
  • 4