【问题标题】:CMake complains about not finding autouic fileCMake 抱怨找不到 autouic 文件
【发布时间】:2020-06-22 20:09:04
【问题描述】:

我正在尝试使用 automoc 和 autouic 在子目录中创建一个“HelloWorld”。

我有一个主目录

cmake_minimum_required(VERSION 3.1.0)

project(helloworld)

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

if(CMAKE_VERSION VERSION_LESS "3.7.0")
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()

find_package(Qt5 COMPONENTS Widgets REQUIRED)

add_subdirectory(main)
add_subdirectory(QtGUI)         

在子目录 main

add_executable(helloworld
    main.cpp
)

include_directories(
        include
        ../QtGUI/include
)

target_link_libraries(helloworld libQtGUI ) 

#include <qapplication.h>
#include <qpushbutton.h>
#include "dialog.h"

int main( int argc, char **argv )
{
  QApplication a( argc, argv );
  Dialog w;
  w.show();
  return a.exec();
}

在 QtGUI/dialog.cpp 文件中

#include "forms/ui_dialog.h"
#include "dialog.h"
#include "dialog.moc"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
 }

Dialog::~Dialog()
{
    delete ui;
}

在 QtGUI/CMakeLists.txt 文件中

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)

#set(AUTOUIC_SEARCH_PATHS  forms)
file(GLOB_RECURSE QOBJECT_SOURCES
    dialog.cpp
)


include_directories(
      ${Qt5Widgets_INCLUDE_DIRS}
      ${CMAKE_BINARY_DIR}
      include
      forms
)

ADD_LIBRARY(libQtGUI ${QOBJECT_SOURCES} )

file(GLOB_RECURSE HEADERS_TO_MOC include/ *.h)
qt5_wrap_cpp(PROCESSED_MOCS
             ${HEADERS_TO_MOC}
             TARGET libQtGUI
             OPTIONS --no-notes) # Don't display a note for the headers which don't produce a moc_*.cpp

target_sources(libQtGUI PRIVATE ${PROCESSED_MOCS}) # This adds generated moc cpps to target


# Use the Widgets module from Qt 5.
target_link_libraries(libQtGUI Qt5::Widgets )

set_target_properties(libQtGUI PROPERTIES OUTPUT_NAME QtGUI
                      )

target_include_directories(libQtGUI PUBLIC
      ${Qt5Widgets_INCLUDE_DIRS}
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

QtGUI/include/dialog.h 是

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QString>
//#include "mythread.h"

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    virtual ~Dialog();
private:
    Ui::Dialog *ui;
};

#endif // DIALOG_H

不使用 AUTOUIC(即当我手动生成 ui_dialog.h 时,它会编译并运行,但我会收到一个

The file includes the moc file "dialog.moc", but does not contain a Q_OBJECT, Q_GADGET or Q_NAMESPACE macro.

警告。当我尝试使用 AUTOUIC 时,我收到一个错误:

fatal error: ui_dialog.h: No such file or directory
 #include "ui_dialog.h"

我做错了什么? 根据文档,

"If a preprocessor #include directive is found which matches <path>ui_<basename>.h, and a <basename>.ui file exists, then uic will be executed to generate the appropriate file. "

我预计会生成forms/ui_dialog.h。

(我在 QtGUI 和 QtGUI/forms 中都有 dialog.ui 文件)

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QPushButton" name="StartButton">
   <property name="geometry">
    <rect>
     <x>150</x>
     <y>60</y>
     <width>89</width>
     <height>25</height>
    </rect>
   </property>
   <property name="text">
    <string>Start</string>
   </property>
  </widget>
  <widget class="QPushButton" name="StopButton">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>60</y>
     <width>89</width>
     <height>25</height>
    </rect>
   </property>
   <property name="text">
    <string>Stop</string>
   </property>
  </widget>
  <widget class="QTextBrowser" name="label">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>60</y>
     <width>121</width>
     <height>31</height>
    </rect>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

【问题讨论】:

    标签: qt cmake qt5


    【解决方案1】:

    ui 文件是在 ${CMAKE_CURRENT_BINARY_DIR} 中生成的,而不是在 ${CMAKE_BINARY_DIR} 中生成的 - 所以包括这个路径,看看它是否有效。

    【讨论】:

    • 抱歉,我没有看到任何变化。
    • 那么 ui_dialog.h 实际上是在你的构建树的某个地方生成的吗?什么时候是 - 具体在哪里?
    【解决方案2】:

    我看不出有重复 UI 文件的理由;看起来第一个用于生成,但第二个预计存在。然后,解决

    1. UI 问题,从 QtGUI 文件夹中删除 dialog.ui(仅将 UI 文件保留在“forms”子文件夹中)
    2. 警告没有问题,因为您在 CPP 文件中没有这些保留字,即您的 CPP 没有可生成的内容;引用的文档位于“源文件处理”部分。只需删除“dialog.moc”包含。它会按预期工作。

    现在它编译和运行没有警告/问题。

    【讨论】:

      猜你喜欢
      • 2010-09-12
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      • 2022-12-28
      • 1970-01-01
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多