Problem with CMAKE_AUTOUIC

I am looking at rewriting some CMake scripts for Qt so Qt5 and Qt6 can be used, specifically those in the VTK Examples.

I have run into a problem with Visual Studio and CMAKE_AUTOUIC. If CMAKE_AUTOUIC is set to ON, Visual Studio cannot find the ui_*.h files even though the files are in build\[projectname]\[projectname]_autogen\include_Debug and the path exists in the C/C++ additional include directories property pages for [projectname].

If I use ninja as the generator everything is OK as the ui_*.h files are in [projectname] folder.

To account for this I have had to do the following:

# Automatically use qt's moc and uic compilers.
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
if (CMAKE_GENERATOR MATCHES "Visual Studio")
    set(CMAKE_AUTOUIC OFF)
endif()

and

# Create code from a list of Qt designer ui files.
if(CMAKE_GENERATOR MATCHES "Visual Studio")
  # We only need to do this for Visual Studio
  if(qt_version EQUAL 6)
    qt_wrap_ui(${KIT}_UI_Hdrs ${${KIT}_UIS})
  else()
    qt5_wrap_ui(${KIT}_UI_Hdrs ${${KIT}_UIS})
  endif()
endif()

Is there a better solution than this?

I am using Visual Studio 16.8.5 and CMake 13.9.5.

I should add that the only thing I could find on the web was this: Can’t include ui-file from linked library using CMake’s autouic](https://stackoverflow.com/questions/60920473/cant-include-ui-file-from-linked-library-using-cmakes-autouic).

Seems that the QtAutoGen developer is not on here…

Though I’m pretty sure there are QtAutoUic tests on Windows with Visual Studio, but maybe this is a hole in the test suite?

Cc: @brad.king

Actually this is not true, they are found in build/projectname]/[projectname]_autogen/include. I must have had some old files in build/[projectname] from when CMAKE_AUTOUIC was set to OFF.

So I’ll continue using the qt_wrap_ui stuff with set(CMAKE_AUTOUIC OFF).

You can get around it by adding the build/[projectname]/[projectname]_autogen/include path to include_directories command, however for Visual Studio you would need to also add ../[projectname]_autogen/include_Debug, ../[projectname]_autogen/include_Release etc.

All of the Qt5Autogen tests in CMake run and pass with the Visual Studio generators. That covers AUTOUIC for Qt5, so this is expected to work in general. Please try to narrow down the failure to a small example.

I don’t think the CMake test suite has been updated to drive those tests for Qt6 though.

@brad, here is a small example Tested using qt5. The CMake variable USE_UIC is ON by default so it fails for me. When USE_UIC is OFF it works.

Remember to delete build/gui (if it exists) before running cmake. If you change the variable USE_UIC delete and re-run cmake.

Qt6_Qt5.zip (7.0 KB)

Thanks. With Qt6_Qt5.zip and VS 16.8 I get

gui\MainWindow.h(4,10): fatal error C1083: Cannot open include file: 'ui_MainWindow.h'

Please strip your example down further to avoid the Qt6/Qt5 switch (use Qt5 only), remove unnecessary Qt components, and remove subdirectories if possible. Then open an issue and attach the example.

@brad.king please see issue 21846. I provided two examples, one where it fails (uses sub-directories) one where it works (no sub-directories).

1 Like

For reference, @jobor pointed out in the issue that this is not a bug.

@amaclean please account for that answer in the VTK Examples.

I definitely will be accounting for that in the VTK Examples! Many thanks again.