0
votes

I am familiar with Qt4 but we are trying to transition to Qt5 and it's being very difficult. I'm trying to create a very simple application, and I had it working using the Qt PRO file, but we need to base it on cmake to keep the build server happy.

The error I get is "invalid use of incomplete type ‘struct Ui::MainWindow’" in the line in mainwindow.cpp where it constructs "ui(new Ui::MainWindow)".

Here are my files (simplified to shorten this post):

mainwindow.h

#include <QtWidgets/QMainWindow>
namespace Ui { class MainWindow; }
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};

mainwindow.cpp

#include "mainwindow.h"
#include "moc_mainwindow.cpp"
MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{ }
MainWindow::~MainWindow()
{ delete ui; }

cmakelists.txt

CMAKE_MINIMUM_REQUIRED( VERSION 2.8.9 FATAL_ERROR )
PROJECT(Test)
set(CMAKE_AUTOMOC TRUE)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
file (GLOB Sources src/*.cpp )
add_executable(Test ${Sources} src/mainwindow.ui )
qt5_use_modules(Test Widgets)

I'm sure I'm missing something obvious, but I've been looking all day and can't figure this one out ...

Looking at the generated moc_mainwindow.cpp (again, some lines truncated to keep this post short):

#include "../src/mainwindow.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
QT_BEGIN_MOC_NAMESPACE
void MainWindow::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
Q_UNUSED(_o);
Q_UNUSED(_id);
Q_UNUSED(_c);
Q_UNUSED(_a);
}
QT_END_MOC_NAMESPACE

I am guessing that MainWindow::qt_static_metacall() is declared in my mainwindow.h in the Q_OBJECT macro. which means I have no idea where this mystical Ui::MainWindow is coming from. Or not.

UPDATE

The problem seems to be that in my mainwindow.cpp, I should be #including "ui_mainwindow.h" instead of "moc_mainwindow.cpp", but there is no "ui_mainwindow.h" being generated, only moc_mainwindow.cpp.

1

1 Answers

2
votes

Turns out my understanding of Qt was completely WRONG. moc doesn't generate those ui files, it's doing other magic to make signals & slots work. The solution is to add this link to CMakeLists.txt:

qt5_wrap_ui(uifiles src/mainwindow.ui)