【发布时间】: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>
【问题讨论】: