当前位置:   article > 正文

CMake使用笔记(带ui文件的Qt程序)_qt cmake autouic 将.h文件和.ui文件放在不同目录ixa

qt cmake autouic 将.h文件和.ui文件放在不同目录ixa

一.包含目录。

set(CMAKE_INCLUDE_CURRENT_DIR ON) #包含编译目录
AUX_SOURCE_DIRECTORY(Dlg Dlg_cpp) #查找指定目录下的所有源文件,然后将结果存进指定变量名

FILE(GLOB file_h ./**)            #加载文件夹下的所有文件
FILE(GLOB Dlg_file Dlg/**)        #以当前CMakeLists路径作为相对路径        
file(GLOB Dlg_h "Dlg/*.h")        #可按路径增加子目录,但在vs中暂只显示一级目录
LIST(APPEND Dlg_file ${Dlg_h} ${Dlg_cpp} ${Dlg_ui}) #变量合并
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二.调用Qt5

2.1 基于环境变量(可为msvc2015_64/lib/cmake/Qt5)

set(CMAKE_PREFIX_PATH $ENV{Qt5}) #系统路径建议直接包含../lib/Qt5/.cmake文件
find_package(Qt5 COMPONENTS Core Gui Widgets Charts REQUIRED) # Qml Quick Network Xml 
  • 1
  • 2

2.2 手动添加(不需本机Qt5环境,但仍需相关cmake文件,但将源码目录进行一定程度剥离简化)

set(Qt5_DIR "${PROJECT_SOURCE_DIR}/Extern/Qt512/lib/cmake/Qt5")
set(Qt5Core_DIR "${PROJECT_SOURCE_DIR}/Extern/Qt512/lib/cmake/Qt5Core")
set(Qt5Gui_DIR "${PROJECT_SOURCE_DIR}/Extern/Qt512/lib/cmake/Qt5Gui")
set(Qt5Widgets_DIR "${PROJECT_SOURCE_DIR}/Extern/Qt512/lib/cmake/Qt5Widgets")
set(Qt5Charts_DIR "${PROJECT_SOURCE_DIR}/Extern/Qt512/lib/cmake/Qt5Charts")
 
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/Extern/Qt512/include/QtCharts")
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/Extern/Qt512/include/QtCore")
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/Extern/Qt512/include/QtGui")
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/Extern/Qt512/include/QtWidgets")
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/Extern/Qt512/include")
LINK_DIRECTORIES("${PROJECT_SOURCE_DIR}/Extern/Qt512/lib")

find_package(Qt5Core)
find_package(Qt5Gui)
find_package(Qt5Widgets)
find_package(Qt5Charts)

SET(file_lib
  Qt5::Core
  Qt5::Gui
  Qt5::Widgets
  Qt5::Charts   
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

2.3 编译ui文件(CMAKE_AUTOUIC,QT5_WRAP_UI)

#set(CMAKE_AUTOMOC ON)   #QObject派生的C++类也需要通过moc进行处理 生成目录下有Demo_automoc文件(去掉才能编过)            
#set(CMAKE_AUTOUIC ON)

file(GLOB file_ui "./*.ui")
QT5_WRAP_UI(ui_FILES ${file_ui} )
  • 1
  • 2
  • 3
  • 4
  • 5

三.其它

3.1 set_property

set_property(GLOBAL PROPERTY USE_FOLDERS ON) #打开允许创建文件夹的开关 BUILD、Zero放在CMakePredefinedTargets
#set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "CMakeTargets") #给cmake自动创建的工程重新命名(此步骤可以省略)
  • 1
  • 2

3.2 设置(release/字符集)

#set(CMAKE_BUILD_TYPE "Release") #Release需加引号
set(CMAKE_BUILD_TYPE Release CACHE STRING "set build type to Release")
add_compile_options("/utf-8")
set(CMAKE_CXX_STANDARD 11)
  • 1
  • 2
  • 3
  • 4

四.示例

project(oeImageCenter) # VERSION 0.1
cmake_minimum_required(VERSION 2.6)

set(CMAKE_BUILD_TYPE "Release") #Release需加引号
add_compile_options("/utf-8")

INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/oeGraphicsView") #解决ui_MainWindow.h找不到MyGraphicsView 
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/Core")
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/Gui")

INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/Extern/opencv450/include")
LINK_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/Extern/opencv450/lib") 
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/Extern/libtiff")
LINK_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/Extern/libtiff")
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/Extern/ImageShop/include")
LINK_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/Extern/ImageShop/lib")
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/Extern/oeBase/include")
LINK_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/Extern/oeBase/lib")

include_directories(${PROJECT_BINARY_DIR}/Gui) #解决找不到ui_oeImageCenter.h

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_AUTOMOC ON)   #QObject派生的C++类也需要通过moc进行处理 生成目录下有Demo_automoc文件(去掉才能编过)    
set(CMAKE_AUTOUIC ON)

set(CMAKE_PREFIX_PATH $ENV{Qt5}) #Qt5为系统环境变量名 Qt512
find_package(Qt5 COMPONENTS Core Gui Widgets Charts REQUIRED) # Qml Quick Network Xml

add_subdirectory(Core)
add_subdirectory(oeGraphicsView)
add_subdirectory(Gui)

SET(file_lib
  Qt5::Core
  Qt5::Gui
  Qt5::Widgets
  Qt5::Charts
  #Qt5::Network
  #Qt5::Xml
  #CTKCore
  #CTKWidgets
  libtiff_64
  opencv_world450#d
  ImageShop
  oeBase
  Core/Release/Core#Debug
  Gui/Release/Gui
  oeGraphicsView/Release/oeGraphicsView #bin目录下    
)

#file(GLOB file_qrc "./*.qrc")  oeImageCenter.qrc
add_executable(${PROJECT_NAME} main.cpp ${Core} ${Gui} ${MyGraphicsView})# main.cpp ${file_}   ${HEADERS} ${SOURCES} ${RESOURCES} ${QMLS}

target_link_libraries(${PROJECT_NAME} ${file_lib}) #${PROJECT_NAME}  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

五.使用总结

1.编译失败,需重新cmake、手动删除moc文件。
2.调用tianya,openssl报错。
Could NOT find OpenSSL
3.cmake工程不建议手动添加文件到项目
VS工程右键添加类文件,声明不调用编译正常,new调用LNK2001报错(猜测cmake会将每个文件记录到工程文件,并按此演绎)。
需重新cmake。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/560901
推荐阅读
  

闽ICP备14008679号