Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt5 and Cmake : strange behavior with wrapp_ui/cpp



  • Hi,
    I try to set up a cmake project for Qt5. In fact it works but not as I want.
    This is a example of my test project

    My project folder
    ┣CMakelists.txt (root)
    ┗src (folder)
       ┣CMakelists.txt (src)
       ┣main.cpp
       ┣mainwindow.h
       ┣mainwindow.cpp
       ┗mainwindow.ui
    

    my cmake root :

    cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)
    
    project(test_1 LANGUAGES CXX)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    set(Qt_PATH "E:/Qt")
    set(Qt_VERSION "5.14.2")
    set(Qt_PROJECT "mingw73_32")
    set(Qt5_DIR ${Qt_PATH}/${Qt_VERSION}/${Qt_PROJECT}/lib/cmake/Qt5 CACHE STRING "")
    
    find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui)
    
    set(project_ui 
        src/mainwindow.ui)
    
    set(project_headers 
        src/mainwindow.h)
    
    set(project_sources 
        src/main.cpp
        src/mainwindow.cpp)
    
    qt5_wrap_ui(project_headers_wrapped ${project_ui})
    qt5_wrap_cpp(project_sources_moc ${project_headers})
    
    # Block 1
    add_executable(${PROJECT_NAME} ${project_headers} ${project_sources} ${project_sources_moc} ${project_headers_wrapped})
    
    # Block 2
    add_executable(${PROJECT_NAME} ${project_headers} ${project_sources})
    add_subdirectory(src)
    
    target_link_libraries(${PROJECT_NAME}
        PUBLIC 
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets)
    
    set(Qt_DLL "libstdc++-6" "libwinpthread-1" "Qt5Core" "Qt5Widgets" "Qt5Gui")
    # dll
    foreach(dll ${Qt_DLL})
        file(
            COPY ${Qt_PATH}/${Qt_VERSION}/${Qt_PROJECT}/bin/${dll}.dll
            DESTINATION ${CMAKE_BINARY_DIR}
        )
    endforeach()
    

    My cmake src :

    set(project_ui 
        mainwindow.ui)
    
    set(project_headers 
        mainwindow.h)
    
    qt5_wrap_ui(project_headers_wrapped ${project_ui})
    qt5_wrap_cpp(project_sources_moc ${project_headers})
    
    target_sources(
        ${PROJECT_NAME}
        PRIVATE
        ${project_sources_moc} ${project_headers_wrapped}
    )
    

    When I enable the Block 1 the cmake project works ! When the Block 2 is enable (so, with the cmake src) the ui_mainwindow.h and moc_mainwindow.cpp will never generated and I don't know why :/

    thank's for a solution. Sorry if the solution is easy to found because I don't understand what is this problem.


  • Qt Champions 2019

    @BrigitteLPB said in Qt5 and Cmake : strange behavior with wrapp_ui/cpp:

    When I enable the Block 1 the cmake project works ! When the Block 2 is enable (so, with the cmake src) the ui_mainwindow.h and moc_mainwindow.cpp will never generated and I don't know why :/

    Because the first version is the correct one, the second not. See the documentation: https://doc.qt.io/qt-5.12/qtwidgets-cmake-qt5-wrap-ui.html#examples

    When you don't add the output files as dependency to your target then noone knows that those files are needed.



  • Hi,
    Thanks for the answer, but I don't understand what you mean. Have you some code to fix my cmake src ? Is it a wrong utilization of target_sources ?


  • Qt Champions 2019

    @BrigitteLPB said in Qt5 and Cmake : strange behavior with wrapp_ui/cpp:

    Have you some code to fix my cmake src ?

    The first version works and is correct.


Log in to reply