Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Qt5 and Cmake : strange behavior with wrapp_ui/cpp
Forum Updated to NodeBB v4.3 + New Features

Qt5 and Cmake : strange behavior with wrapp_ui/cpp

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
4 Posts 2 Posters 515 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BrigitteLPB
    wrote on last edited by BrigitteLPB
    #1

    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.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • B Offline
        B Offline
        BrigitteLPB
        wrote on last edited by BrigitteLPB
        #3

        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 ?

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved