2
votes

I am trying to make a 'HelloWorld' in subdirectories, using automoc and autouic.

I have a main directory with

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)         

and in subdirectory main

add_executable(helloworld
    main.cpp
)

include_directories(
        include
        ../QtGUI/include
)

target_link_libraries(helloworld libQtGUI ) 

and

#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();
}

In file 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;
}

in file 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>
)

and QtGUI/include/dialog.h is

#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

Without using AUTOUIC (i.e when I generate ui_dialog.h manually, it compiles and runs, but I receive a

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

warning. When I attempt to use AUTOUIC, I receive an error:

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

What do I wrong? According to the docs,

"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. "

I expected that forms/ui_dialog.h will be generated.

(I have dialog.ui file both in QtGUI and QtGUI/forms)

<?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>
2

2 Answers

1
votes

The ui file is generated in ${CMAKE_CURRENT_BINARY_DIR}, not ${CMAKE_BINARY_DIR} - so include this path and see if it works.

0
votes

I don't see reason to have duplicate UI files; looks like the first one is used to generate but second one is expected to exists. Then, to solve

  1. UI issue, remove dialog.ui from QtGUI folder (keep only UI file in 'forms' subfolder)
  2. the warning is OK because you don't have these reserved words in CPP file i.e. there is nothing to generate from your CPP; the referred documentation is in 'Source file processing' section. Just remove that 'dialog.moc' include. And it will work as expected.

Now it compiles and run without warnings/issues.