使用的Qt版本为6.8.1
, cmake版本为3.31.0-rc1
。
遇到问题原因
原本的目录结构比较乱,代码文件一多就很不好找,在对文件进行分类管理的过程中发现*ui
文件无法生成ui_*.h
有问题部分的cmake
本来想使用qt_wrap_ui
和set(CMAKE_AUTOUIC ON)
让他自动生成ui_*.h
的,但是失败了
cmake_minimum_required(VERSION 3.16)
project(Usim LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_PREFIX_PATH "D:/Qt/Qt6.8.1/6.8.1/msvc2022_64")
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Charts)
qt_standard_project_setup()
set(SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)
set(MOUDLE_DIR ${SOURCE_DIR}/moudle)
set(UI_DIR ${PROJECT_SOURCE_DIR}/ui)
set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
set(VTK_DIR "C:/Program Files (x86)/VTK/include/vtk-9.4")
include_directories(${UI_DIR} ${VTK_DIR} ${SOURCE_DIR} ${INCLUDE_DIR} ${MOUDLE_DIR})
# 处理 .ui 文件
file(GLOB UI_FILES ${UI_DIR}/*.ui)
qt_wrap_ui(UI_HEADERS ${UI_FILES})
message(STATUS "UI_HEADERS: "${UI_HEADERS})
# 处理源文件和头文件
file(GLOB PROJECT_SOURCES
${SOURCE_DIR}/main.cpp
)
file(GLOB MOUDLE_SOURCES
${MOUDLE_DIR}/*.cpp
${INCLUDE_DIR}/*.h
)
list(APPEND PROJECT_SOURCES ${UI_HEADERS} ${MOUDLE_SOURCES})
正确的完整cmake写法
使用list(APPEND CMAKE_AUTOUIC_SEARCH_PATHS ${UI_DIR})
替换file(GLOB UI_FILES ${UI_DIR}/*.ui)
和qt_wrap_ui(UI_HEADERS ${UI_FILES})
即可。
cmake_minimum_required(VERSION 3.16)
project(Usim LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_PREFIX_PATH "D:/Qt/Qt6.8.1/6.8.1/msvc2022_64")
set(CMAKE_AUTOUIC ON)
set(VERSION_MAJOR 2)
set(VERSION_MINOR 0)
set(VERSION_PATCH 1)
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Charts)
qt_standard_project_setup()
# 设置源文件、头文件、ui文件路径
set(SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)
set(SOURCE_MOUDLE_DIR ${SOURCE_DIR}/moudle)
set(SOURCE_UI_DIR ${SOURCE_DIR}/ui)
set(UI_DIR ${PROJECT_SOURCE_DIR}/ui)
set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
set(VTK_DIR "C:/Program Files (x86)/VTK/include/vtk-9.4")
include_directories(${UI_DIR} ${VTK_DIR} ${SOURCE_DIR} ${INCLUDE_DIR} ${SOURCE_MOUDLE_DIR} ${SOURCE_UI_DIR})
file(GLOB PROJECT_SOURCES
${SOURCE_DIR}/main.cpp
${SOURCE_MOUDLE_DIR}/*.cpp
${SOURCE_UI_DIR}/*.cpp
)
file(GLOB INCLUDE_SOURCES
${INCLUDE_DIR}/*.h
)
list(APPEND CMAKE_AUTOUIC_SEARCH_PATHS ${UI_DIR})
list(APPEND PROJECT_SOURCES ${INCLUDE_SOURCES})
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
# 设置 CGNS 库
if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(CGNS_LIB_PATH "${PROJECT_SOURCE_DIR}/libs/debug")
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
set(CGNS_LIB_PATH "${PROJECT_SOURCE_DIR}/libs/release")
endif()
set(CGNS_LIBS
cgns libhdf5 libszip libzlib
)
foreach(cgns_lib ${CGNS_LIBS})
add_library(${cgns_lib} STATIC IMPORTED)
set_target_properties(${cgns_lib} PROPERTIES IMPORTED_LOCATION "${CGNS_LIB_PATH}/${cgns_lib}.lib")
endforeach()
# 设置 VTK 库
set(VTK_LIB_PATH "C:/Program Files (x86)/VTK/lib")
set(VTK_LIBS
vtksys-9.4 vtkImagingCore-9.4 vtkIOImage-9.4 vtkGUISupportQt-9.4
vtkCommonCore-9.4 vtkCommonDataModel-9.4 vtkCommonExecutionModel-9.4
vtkFiltersCore-9.4 vtkFiltersGeneral-9.4 vtkFiltersGeometry-9.4 vtkFiltersModeling-9.4 vtkFiltersSources-9.4
vtkRenderingCore-9.4 vtkRenderingAnnotation-9.4 vtkRenderingContext2D-9.4 vtkRenderingOpenGL2-9.4
vtkInteractionStyle-9.4 vtkInteractionWidgets-9.4 vtkInteractionImage-9.4
)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(VTK_LIBS_DEBUG "")
foreach(lib ${VTK_LIBS})
list(APPEND VTK_LIBS_DEBUG "${lib}d")
endforeach()
set(VTK_LIBS ${VTK_LIBS_DEBUG})
endif()
foreach(vtk_lib ${VTK_LIBS})
add_library(${vtk_lib} STATIC IMPORTED)
set_target_properties(${vtk_lib} PROPERTIES IMPORTED_LOCATION "${VTK_LIB_PATH}/${vtk_lib}.lib")
endforeach()
# 链接库
target_link_libraries(${PROJECT_NAME}
PUBLIC
Qt6::Core Qt6::Gui Qt6::Widgets Qt6::Charts
${CGNS_LIBS}
${VTK_LIBS}
)
# 定义翻译文件
set(TRANSLATIONS
"${PROJECT_SOURCE_DIR}/translations/zh_CN.ts"
)
find_program(LUPDATE_EXECUTABLE lupdate)
find_program(LRELEASE_EXECUTABLE lrelease)
foreach(_ts_file ${TRANSLATIONS})
execute_process(COMMAND ${LUPDATE_EXECUTABLE} -recursive ${CMAKE_SOURCE_DIR} -ts ${_ts_file})
execute_process(COMMAND ${LRELEASE_EXECUTABLE} ${_ts_file})
endforeach()
# 资源文件
qt_add_resources(PROJECT_SOURCES resources/Resource.qrc)
# 生成的是windows下的可执行文件
set_target_properties(${PROJECT_NAME}
PROPERTIES
WIN32_EXECUTABLE TRUE
)
标签:set,cmake,SOURCE,ui,下时,9.4,UI,DIR
From: https://www.cnblogs.com/sleeeeeping/p/18675357