CMake Qt5无法为ui文件AUTOUIC生成头文件

时间:2017-07-07 09:18:48

标签: c++ cmake qt5

我无法使用cmake 3.5.2和Qt 5.9为我的ui文件生成标题。

我的CMakeFileLists.txt:

cmake_minimum_required(VERSION 3.5)
project( fc_app )
message( STATUS "Configuring project")


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

message( STATUS "search OpenCV")
find_package(
    OpenCV
    3.2.0
    REQUIRED
)

message( STATUS "search Qt")
find_package(
    Qt5
    5.5.1
    REQUIRED
        Core
        Gui
        Widgets
        Multimedia
)

message( STATUS "search Boost")
find_package(
    Boost
    1.58.0
    REQUIRED
)


file( GLOB_RECURSE source_files src/* )
file( GLOB_RECURSE header_files include/* )
file( GLOB_RECURSE ui_files ui/* )
file( GLOB_RECURSE res_files res/* )

add_executable(
    fc_app
    ${source_files}
    ${header_files}
    ${ui_files}
)

target_link_libraries(
    fc_app
    ${OpenCV_LIBS}
    Qt5::Widgets
    Qt5::Multimedia
    ${Boost_LIBRARIES}
)

当我在root / build /目录中运行cmake .. & make时,我收到以下错误(在make中)。 ui文件是一个简单的QMainWindow,里面有2个按钮,所以我不解释为什么ui_mainwindow.h的生成失败了。我试图用其他版本的Qt5 Designer重新制作ui文件。

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

2 个答案:

答案 0 :(得分:1)

我不知道这个问题是针对特定版本的CMake,但是有一个问题并且已经发布,如果你的ui文件有大写,例如MuyWidget.ui,那么你有与CMake的错误。对我而言,解决方案是将ui文件命名为mywidget.ui,而不是MyWidget.ui

答案 1 :(得分:1)

对于AUTOUIC来说,CMake和QT有一个怪癖。如果ui包含在源文件的第一行上,则CMake将自动扫描ui文件包含的文件claimed

以下设置:

CMakeLists.txt

project("proj")

set(CMAKE_PREFIX_PATH "$ENV{QTDIR}")
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)

set(CMAKE_INCLUDE_CURRENT_DIR "YES")
set(CMAKE_AUTOMOC "YES")
set(CMAKE_AUTOUIC "YES")

add_executable("proj" MACOSX_BUNDLE main.cpp)
target_link_libraries("proj" Qt5::Core Qt5::Widgets)

form.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
 </widget>
 <resources/>
 <connections/>
</ui>

使用main.cpp编译时无法编译:

1. #include "ui_form.h"
2.
3. int main() {
4. }
$ cmake --build .
Scanning dependencies of target proj_automoc
[ 25%] Automatic moc and uic for target proj
Generating moc compilation proj_automoc.cpp
[ 25%] Built target proj_automoc
Scanning dependencies of target proj
[ 50%] Building CXX object CMakeFiles/proj.dir/main.cpp.o
main.cpp:1:10: fatal error: 'ui_form.h' file not found
#include "ui_form.h"
         ^
1 error generated.
make[2]: *** [CMakeFiles/proj.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/proj.dir/all] Error 2
make: *** [all] Error 2

但是,如果您的ui包含不在第一行:

1. 
2. #include "ui_form.h"
3. int main() {
4. }
$ cmake --build .
[ 25%] Automatic moc and uic for target proj
Generating ui header ui_form.h
[ 25%] Built target proj_automoc
Scanning dependencies of target proj
[ 50%] Building CXX object CMakeFiles/proj.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/proj.dir/proj_automoc.cpp.o
[100%] Linking CXX executable proj.app/Contents/MacOS/proj
[100%] Built target proj

UPD。 用于扫描包含的This regular expression是罪魁祸首:

this->UicRegExpInclude.compile("[\n][ \t]*#[ \t]*include[ \t]+"
                               "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");

它仍然存在于最新版本3.10.2中,但已在master中修复:

Uic_.RegExpInclude.compile("(^|\n)[ \t]*#[ \t]*include[ \t]+"
                           "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");