CMake Qt UIC失败

ct3nt3jp  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(174)

我目前正在将我的项目从qmake移植到CMake,我遇到了一个Qt UIC的问题,它试图处理一个不存在的UI文件,而不是我希望它处理的实际文件。
我有以下文件层次结构:

.
|___ CMakeLists.txt
|___ MyProject.pro
|___ mainwindow.ui
|___ resource.qrc
|___ source
|    |___ mainwindow.cpp
|    |___ *.cpp
|___ include
|    |___ mainwindow.h
|    |___ *.h

下面是我的CMakeLists.txt:

cmake_minimum_required(VERSION 3.2)

# Project name
project(project)

# Tell CMake to compile with C++11
set(CMAKE_CXX_STANDARD 11)

# Tell CMake to run moc when needed.
set(CMAKE_AUTOMOC ON)

# Tell CMake to run uic when needed.
set(CMAKE_AUTOUIC ON)

# Tell CMake to run rcc when needed
set(CMAKE_AUTORCC ON)

# Moc generated files are located in the current dir so we need to tell CMake to look for them.
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Find Qt5
find_package(Qt5 COMPONENTS Widgets Core Gui OpenGL REQUIRED)
# Add Qt5 definitions and includes to build libraries. 
# Widgets add Widgets Core and Gui
add_definitions(${Qt5Widgets_DEFINITIONS})
include_directories(${Qt5Widgets_INCLUDES})

add_definitions(${Qt5OpenGL_DEFINITIONS})
include_directories(${Qt5OpenGL_INCLUDES})

# Find OpenGL
find_package(OpenGL REQUIRED)

# Set include directories
include_directories(${CMAKE_SOURCE_DIR}/include
    
# Use Qt5 ressources
set(RESOURCE resources.qrc)
# Use Qt5 ui
set(UI mainwindow.ui)

# Adding sources
set(SOURCES
    source/main.cpp
    source/mainwindow.cpp
    source/octree.cpp
    source/mesh.cpp
    source/pgm3d.cpp
    source/glwidget.cpp
    source/camera.cpp
    source/scene.cpp 
    source/light.cpp 
    source/obj.cpp 
    source/alignedbox3f.cpp 
    source/wireboundingbox.cpp

    include/mainwindow.h
    include/utils.h
    include/octree.h
    include/mesh.h
    include/pgm3d.h
    include/glwidget.h
    include/camera.h
    include/scene.h
    include/model3d.h
    include/light.h
    include/obj.h
    include/alignedbox3f.h
    include/wireboundingbox.h)

add_executable(${PROJECT_NAME} ${UI} ${RESOURCE} ${SOURCES} )
target_link_libraries(${PROJECT_NAME} ${Qt5Widgets_LIBRARIES} ${Qt5OpenGL_LIBRARIES} ${OPENGL_LIBRARIES})

我得到了以下错误:

File '/*/project/source/mainwindow.ui' is not valid
AUTOUIC: error: process for ui_mainwindow.h needed by
 "/*/project/source/mainwindow.cpp"
failed:
File '/*/project/source/mainwindow.ui' is not valid

这个错误是完全合乎逻辑的,因为我的源文件夹不包含UI文件。我试着 Package UI而不是使用AUTOUIC,它也不起作用。

qacovj5a

qacovj5a1#

目标的AUTOUIC属性(在创建目标时从变量CMAKE_AUTOUIC设置)定义了生成器的行为-当启用AUTOUIC属性时,CMake将扫描源文件中是否包含ui文件(如#include "ui_*.h"#include <ui_*.h>),并将自动使用uic工具处理它们。
在CMake 3.9中,添加了另一个目标属性AUTOUIC_SEARCH_PATHS。类似的问题已经被问到here
另一个选择是禁用AUTOUIC并使用qt5_wrap_ui,并使用UI文件的完整路径:

set(CMAKE_AUTOUIC OFF)
set(UI ${CMAKE_CURRENT_LIST_DIR}/mainwindow.ui)
...
qt5_wrap_ui(UI_HEADERS ${UI})
add_executable(${PROJECT_NAME} ${UI_HEADERS} ${RESOURCE} ${SOURCES} )

相关问题