2

I have a simple setup with main.cpp, mainwindows.cpp, mainwindow.h, and mainwindow.ui with this cmake file :

cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
project(test_qt_with_cmake)

set(CMAKE_CXX_STANDARD 11) # use c++11

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_PREFIX_PATH "C:/Qt/5.8/msvc2015_64")

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt5Widgets)

set(PROJECT_INCLUDE_DIR "${PROJECT_SOURCE_DIR}")

######## PROJECT DEPENDENCIES #########
if(WIN32)
    set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${PROJECT_SOURCE_DIR}/windows)
endif()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")

#=====================================

include_directories(
    ${PROJECT_INCLUDE_DIR}
)

add_executable(test_qt WIN32 main.cpp mainwindow.cpp mainwindow.h)
target_link_libraries(test_qt Qt5::Widgets)

add_custom_command(TARGET test_qt POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${PROJECT_SOURCE_DIR}/windows/bin"
        $<TARGET_FILE_DIR:test_qt>)

The ui_xxx.h is generated the first time i build the project and regenerated if the .ui file and one of source file is changed. What i would like is to get the ui_xxx.h to get regenerated everytime .ui file is changed with or without source change.

I've taken a look at https://github.com/euler0/mini-cmake-qt but the example_automoc project is always out of date and vs always ask to rebuild the project.

Is there something wrong in my cmake file? or is it a bug in cmake?

note: i'm using qt 5.8 with cmake 3.6 on windows 10 using visual studio 14 2015 generator

update : removing set(CMAKE_AUTOUIC ON) and specifying the ui file with :

qt5_wrap_ui(UI_HEADERS mainwindow.ui) 

seems to solve the problem. The original question is still valid, which is how to make this work without specifying the .ui file (by calling qt5_wrap_ui) ?

bysreg
  • 793
  • 1
  • 9
  • 30
  • I can tell you I don't have this issue. However I don't use automoc or autouic. I explicitly list every file in my `CMakeLists.txt` and tell it exactly what files I wan't to moc ... – drescherjm Feb 27 '17 at 00:29
  • @drescherjm would you mind sharing your simplified cmake file? – bysreg Feb 27 '17 at 00:32
  • The closest example would be: https://cmake.org/Wiki/CMake/Tutorials/Qt although this is Qt4, the Qt5 macros are named similarly. I mean prefix with QT5 instead of QT4. – drescherjm Feb 27 '17 at 00:40
  • Thanks! using qt5_wrap_ui seems to solve the problem. Though that means the CMAKE_AUTOUIC somehow doesn't work as it's supposed to – bysreg Feb 27 '17 at 00:43
  • How do you want CMake to know it must generate ui_xxx.h if you do not specify that you have a ui file? Thus you always need to specify the .ui file. – oLen Feb 27 '17 at 14:23
  • `CMAKE_AUTOUIC` is supposed to do that. – drescherjm Feb 27 '17 at 14:26
  • This is a known CMake issue with `CMAKE_AUTOUIC`, see my response [here](https://stackoverflow.com/a/60305798/3987854). – Kevin Feb 19 '20 at 17:20
  • Update: the referenced issue was fixed [on 2021 April 10th by this merge](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5999). – starball Sep 01 '22 at 20:43

0 Answers0