qtqt-creatormoc

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


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?


Solution

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