qtcmakeuic

CMake Qt5 and AUTOUIC not rebuilding when .ui file touched


I have a Qt5 project, using CMake with AUTOUIC, AUTOMOC, and AUTORCC.

My problem is that if I change one of the .ui files, I expect UIC to run and produce the corresponding ui_XXX.h file. It doesn't. I have the .ui files listed in my add_library(... Foo1.ui Foo2.ui) declaration.

This is on Windows with Visual Studio 2019. I am using the VS solution file produced my CMake. As far as I can tell, the only time Auto UIC runs is if it is building the library; touch any source file, and everything builds as expected. Touch just a .ui file and build, and it doesn't build anything.

Building the application on Linux works as expected.

We just migrated the project to CMake for a common build system between Windows and Linux and quirks like this are annoying some people on the team and we would like to resolve them.


Solution

  • This was a known CMake issue, and was fixed by this merge request (5999). The issue mentions Visual Studio 2017 specifically, but the problem will be the same for other Visual Studio generators.

    If you can't update to the version where it was fixed, one work-around is to use the qt5_wrap_ui() command instead of relying on CMAKE_AUTOUIC. This way, a UIC rule is created for each .ui file explicitly listed:

    qt5_wrap_ui(MY_LIB_UI_FILES Foo1.ui Foo2.ui ...)
    add_library(MyLib 
        # ... 
        ${MY_LIB_UI_FILES}
    )