0

Below is my desired folder structure :

MyProject
  CMakeLists.txt

  include
       mainwindow.h

  src
       mainwindow.cpp
       main.cpp

  forms
       mainwindow.ui

This is my desired folder structure. Below is my CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(MyProject VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)

# Add the ui files
qt6_wrap_ui(UI_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/forms/mainwindow.ui
)

# Include the directories
include_directories(INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/include)

set(PROJECT_SOURCES
         ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src/mainwindow.cpp
        ${UI_FILES}
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(MyProject
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET MyProject APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(MyProject SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(MyProject
            ${PROJECT_SOURCES}
        )
    endif()
endif()

# Include the forms directory
target_link_libraries(MyProject PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
  set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.MyProject)
endif()
set_target_properties(MyProject PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS MyProject
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(MyProject)
endif()

I set out to achieve two things when I made the CMake file this way:

  1. In my cpp file, I should be able to
#include "mainwindow.h"

instead of

#include "../include/mainwindow.h"

This was achieved as my main.cpp and mainwindow.cpp has the desired include.

  1. To achieve this folder structure, but the below is the error:
"SRC:/src/mainwindow.cpp"
includes the uic file "ui_mainwindow.h",
but the user interface file "mainwindow.ui"
could not be found in the following directories
  "SRC:/src"

Note : I am using QT Creator.

2
  • On the one side you use cmake automoc (set(CMAKE_AUTOUIC ON)) but then you do the uic stuff by yourself (qt6_wrap_ui(UI_FILES) which is wrong - only use one of them. Since you use a custom layout for your ui & src organization, you might only be able to use qt6_wrap_ui() and not automoc. Commented May 1, 2024 at 6:40
  • Maybe also setting cmake.org/cmake/help/latest/prop_tgt/… to the appropriate folder helps to make AUTOUIC work. Commented May 1, 2024 at 10:50

2 Answers 2

0

I don't know why, but was able to resolve this issue by moving the mainwindow.ui file to the same directory with the source files. So the structure will be the following:

MyProject
  CMakeLists.txt

  include
       mainwindow.h

  src
       mainwindow.cpp
       main.cpp
       mainwindow.ui
Sign up to request clarification or add additional context in comments.

Comments

0

add this in the top of you CMakeLists.txt

set(CMAKE_AUTOUIC_SEARCH_PATHS "YOURPATH")

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.