Reputation: 71
I'm tring to create an interface with Qt to oblige any subclass to implements two main methods set and get title. But when I try to compile it I get a weird error message that says something about qt_check_for_QOBJECT_macro and staticMetaObject. In mainwindow.cpp I have to cast any page to the interface so I can rely on getter and setter methods. I don't see any other way to do that.
this is my code:
//IPage.h
#ifndef IPAGE_H
#define IPAGE_H
#include <QString>
class IPage
{
public:
virtual QString title()=0;
virtual void setTitle(QString t)=0;
};
#endif // IPAGE_H
//buildings.h:
#ifndef BUILDINGS_H
#define BUILDINGS_H
#include "IPage.h"
#include <QDialog>
class Buildings : public IPage, public QDialog
{
Q_OBJECT
private:
QString m_title;
//stuff...
};
#endif
//buildings.cpp
//stuff...
void Buildings::setTitle(QString t)
{
m_title = t;
setWindowTitle(t);
}
QString Buildings::title()
{
return m_title;
}
//mainwindow.cpp:
QMdiSubWindow *MainWindow::findChild(const QString &title)
{
foreach (QMdiSubWindow *window, mdiArea->subWindowList()) {
IPage *child = qobject_cast<IPage *>(window->widget()); /*line 178*/
if (child->title() == title)
return window;
}
return 0;
}
and I get this error message when I compile my code:
In file included from c:\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtCore/qabstractanimation.h:45,
from c:\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtCore/QtCore:3,
from c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtGui/QtGui:3,
from mainwindow.cpp:1:
c:\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtCore/qobject.h: In function 'T qobject_cast(QObject*) [with T = IPage*]':
mainwindow.cpp:178: instantiated from here
c:\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtCore/qobject.h:378: error: 'class IPage' has no member named 'qt_check_for_QOBJECT_macro'
c:\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtCore/qobject.h:380: error: 'class IPage' has no member named 'staticMetaObject'
mingw32-make.exe[1]: Leaving directory `D:/Dropbox/Programmi/Qt/Scadenziario'
mingw32-make.exe[1]: *** [build/o/mainwindow.o] Error 1
mingw32-make.exe: *** [debug] Error 2
01:23:26: The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
Error while building project scadenziario (target: Desktop)
When executing build step 'Make'
I can't understand error message. I tried to google it, but I can't find any useful informations. Any help will be appreciated, thanks in advance.
Upvotes: 7
Views: 3225
Reputation: 4828
The meta object compiler requires that the first class you inherit from be the QObject-derived class.
So you should change:
class Buildings : public IPage, public QDialog
to:
class Buildings : public QDialog, public IPage
Upvotes: 11
Reputation: 22366
To add to air-dex's answer:
Because of meta data provided by QObject
, when casting between QObject
s RTTI isn't needed which makes the operation faster (and if you use this exclusively, you can turn off RTTI support in your executable making it smaller). However it is not designed to be used the way you are trying to - as a replacement for dynamic_cast
. So just change:
IPage *child = qobject_cast<IPage *>(window->widget()); /*line 178*/
to:
IPage *child = dynamic_cast<IPage *>(window->widget()); /*line 178*/
Upvotes: 0
Reputation: 4178
When you use qobject_cast<T *>
, T
has to inherit from QObject
. In your case, T
= IPage
but IPage
does not inherit QObject
. That's why you get the error.
Upvotes: 2