2012-07-14 86 views
2

我正在创建一个Qt接口来强制任何子类实现两个主要的方法集和标题。但是,当我尝试编译它时,我收到一个奇怪的错误消息,它提到了有关qt_check_for_QOBJECT_macro和staticMetaObject的一些信息。 在mainwindow.cpp中,我必须将任何页面转换为接口,以便我可以依赖getter和setter方法。 我没有看到任何其他方式来做到这一点。Qt纯虚函数错误

这是我的代码:

//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; 
} 

,当我编译我的代码,我收到此错误信息:

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' 

我无法理解的错误消息。我试图谷歌它,但我找不到任何有用的信息。 任何帮助将不胜感激,在此先感谢。

回答

2

当您使用qobject_cast<T *>时,T必须从QObject继承。在你的情况下,T = IPageIPage不继承QObject。这就是你得到错误的原因。

+0

我怎样才能解决这个错误? IPage它只是一个界面。我不需要它从QObject继承。我需要确定任何IPage的实现都有getter和setter方法。 – Al79 2012-07-15 00:32:25

+0

对于使用'qobject_cast ','IPage' **必须从'QObject'继承:'class IPage:public QObject'。如果你不想这样做,你将不得不在你的'IPage'类中实现一个新的方法'虚拟IPage * IPage :: fromQObject(QObject * obj)= 0;'(或类似的东西)在C++中不存在)。 – 2012-07-15 00:52:11

0

为了增加空气-DEX的回答是:

因为QObject提供的元数据,QObject小号RTTI之间的转换是没有必要使操作速度更快时(如果你使用这个专门,您可以打开关闭可执行文件中的RTTI支持,使其更小)。然而,它是而不是设计为使用你想要的方式 - 作为dynamic_cast的替代品。因此,只要改变:

IPage *child = qobject_cast<IPage *>(window->widget()); /*line 178*/ 

到:

IPage *child = dynamic_cast<IPage *>(window->widget()); /*line 178*/ 
4

meta object compiler要求从继承一流是QObject-derived class

所以,你应该改变:

class Buildings : public IPage, public QDialog

到:

class Buildings : public QDialog, public IPage

+0

此解决方案正在运行。谢谢。但你得到了一个错字,我们应该读:class Buildings:public QDialog,public IPage – Vincent 2013-08-26 21:26:44

+0

谢谢,我不能相信我没有注意到 – 2013-10-12 06:35:14