Qt using CMake: ui_mainwindow.h: No such file or directory

13,405

Solution 1

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.

Solution 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.

Solution 3

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.

Share:
13,405
Daniel Arnett
Author by

Daniel Arnett

Updated on June 09, 2022

Comments

  • Daniel Arnett
    Daniel Arnett about 2 years

    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
    Daniel Arnett about 8 years
    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.
  • rbaleksandar
    rbaleksandar about 8 years
    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.
  • Adrian Maire
    Adrian Maire about 7 years
    I guess @Sami answer complete this one: set(CMAKE_AUTOUIC ON) is required so this answer works.
  • j4x
    j4x about 7 years
    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.
  • RAM
    RAM about 5 years
    Setting CMAKE_AUTOUIC to ONonly 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.
  • KungPhoo
    KungPhoo over 4 years
    You might have to set set(CMAKE_AUTOUIC ON) before add_executable().