说明:个人理解,未必正确
参考了下面的网址
http://cn.voidcc.com/question/p-wpcanvtj-tn.html
http://cn.voidcc.com/question/p-wpcanvtj-tn.html文章来源:https://www.toymoban.com/news/detail-756220.html
cmake中将set(CMAKE_AUTOUIC ON)打开
set(CMAKE_AUTOUIC ON) # 自动处理ui文件, 自动处理ui文件是有前提的, 必须在需要编译的头文件(add_executable)中的某个头文件中包含ui_xxx.h(比如#include "ui_model002.h")
set(CMAKE_AUTOMOC ON) # 自动调用moc(元对象编译器)
set(CMAKE_AUTORCC ON) # 自动处理资源
头文件添加到add_executable中
add_executable(qt_test_001
# main.cpp
# mainwindow.cpp
# mainwindow.h
# mainwindow.ui
# model002.ui
# models/model001.ui
# models/model001.cpp models/model001.h
${SRC_LIST}
)
头文件中包含ui_xxx.h, xxx是ui文件的名字, 比如ui_model001.h
//
// Created by mayn on 2023-08-08.
//
#ifndef QT_TEST_001_MODEL001_H
#define QT_TEST_001_MODEL001_H
#include "ui_model001.h" //只有包含了这个, cmake中的CMAKE_AUTOUIC才会生效。且这一行不要放在第一行, 好像是cmake的bug
class model001 {
};
#endif //QT_TEST_001_MODEL001_H
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GUDEGbZx-1691477530154)(/api/file/getImage?fileId=64d1e2ce2a7f2605f9000610)]文章来源地址https://www.toymoban.com/news/detail-756220.html
完整的cmake文件
cmake_minimum_required(VERSION 3.5)
project(qt_test_001 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON) # 自动处理ui文件, 自动处理ui文件是有前提的, 必须在需要编译的头文件(add_executable)中的某个头文件中包含ui_xxx.h(比如#include "ui_model002.h")
set(CMAKE_AUTOMOC ON) # 自动调用moc(元对象编译器)
set(CMAKE_AUTORCC ON) # 自动处理资源
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#;-list of directories specifying installation prefixes to be searched by the find_package(),
# find_program(), find_library(), find_file(), and find_path() commands.
#Each command will add appropriate subdirectories (like bin, lib, or include) as specified
# in its own documentation
# By default this is empty. It is intended to be set by the project.
set(CMAKE_PREFIX_PATH "D:/qt/anzhuang/5.14.2/msvc2017")
# 设置MSVC编译编码
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.
#if(ANDROID)
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
# set(ANDROID_EXTRA_LIBS
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
# endif()
#endif()
find_package(Qt5 COMPONENTS Widgets REQUIRED)
# 查找当前目录下的所有源文件,放到SRC_LIST变量中
#AUX_SOURCE_DIRECTORY(. SRC_LIST)
# 查找当前目录下的所有源文件,放到SRC_LIST变量中
# 如果我们不但在当前目录需要引入,还需要在当前目录子目录引入了,这里就直接使用GLOB_RECURSE(递归)
#FILE(GLOB SRC_LIST . *.cpp *.ui)
file(GLOB SRC_LIST ${PROJECT_SOURCE_DIR}/*.cpp
${PROJECT_SOURCE_DIR}/*.h
# ${PROJECT_SOURCE_DIR}/*.ui # 这里面不要包含ui文件
${PROJECT_SOURCE_DIR}/models/*.cpp
${PROJECT_SOURCE_DIR}/models/*.h
# ${PROJECT_SOURCE_DIR}/models/*.ui # 这里面不要包含ui文件
)
FOREACH(F ${SRC_LIST})
MESSAGE(STATUS ${F})
ENDFOREACH(F)
if(ANDROID)
add_library(qt_test_001 SHARED
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
else()
add_executable(qt_test_001
# main.cpp
# mainwindow.cpp
# mainwindow.h
# mainwindow.ui
# model002.ui
# models/model001.ui
# models/model001.cpp models/model001.h
${SRC_LIST}
)
endif()
target_link_libraries(qt_test_001 PRIVATE Qt5::Widgets)
到了这里,关于qt中cmake自动处理ui文件的前提的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!