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

brjng4g3  于 2022-11-24  发布在  其他
关注(0)|答案(3)|浏览(500)

我不能用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}
)

当我在/build/根目录下运行cmake .. & make时,我得到了以下错误(在make中)。ui文件是一个简单的QMainWindow,里面有两个按钮,所以我不明白为什么ui_mainwindow. h的生成失败了。我也试着用其他版本的Qt 5 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
vhmi4jdf

vhmi4jdf1#

CMake和QT在AUTOUIC上有一个怪癖。它是claimed,CMake会自动扫描文件中的ui文件包含,尽管如果ui包含在源文件的第一行上,它就不起作用。
因此,以下设置:
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进行编译,该main.cpp具有:
第一次
但是,如果您的ui include不在第一行:
第一个
UPD。这个用来扫描include的正则表达式是罪魁祸首:

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

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

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

laximzn52#

我不知道这个问题是否是针对CMake的特定版本,但a也有同样的问题,并发布了如果您的ui文件有大写字母,例如MuyWidget.ui,那么您在CMake中就会有这个错误。我的解决方案是将ui文件用小写字母命名为mywidget.ui,而不是MyWidget.ui

8yoxcaq7

8yoxcaq73#

对我来说,问题是在我的源代码中,我包含了生成的ui文件,我使用的是Ui_.h而不是ui_.h
在解析源文件以确定要自动生成的用户界面文件时,区分大小写。

相关问题