Qt前向声明会产生错误

Dew*_*rld 0 c++ qt qt4

在编写类TemplateHandler时,请说我使用TemplateHandler.h(对于我的标题)和TemplateHandler.cpp(对于声明).喜欢

// Templatehandler.h

#ifndef TEMPLATEHANDLER_H
#define TEMPLATEHANDLER_H

#include <QObject>   // Forward declaration of QObject generates error

class QListView;     // Forward declarations
class QTextEdit;
class QModelIndex;
class QStringListModel;

class TemplateHandler : public QObject
{
  Q_OBJECT
  public:
    TemplateHandler(QListView *view, QTextEdit *textEdit, QObject *parent);
    virtual ~TemplateHandler();

  private:
    QTextEdit *mTextEdit;
    QStringListModel *mModel;
};

#endif
Run Code Online (Sandbox Code Playgroud)

和来源

#include "templatehandler.h"
#include <QListView>             // Inclusion of lib
#include <QTextEdit>
#include <QObject>
#include <QStringListModel>

TemplateHandler::TemplateHandler(QListView *view, QTextEdit *textEdit, QObject *parent) : QObject(parent), mTextEdit(textEdit)
{
    mModel = new QStringListModel(this);
    QStringList templates;
    templates << "<html>" << "</html>" << "<body>" << "</body>";
    mModel->setStringList(templates);
    view->setModel(mModel);
    connect(view, SIGNAL(clicked(const QModelIndex&)), SLOT(insertText(const QModelIndex&)));
}

TemplateHandler::~TemplateHandler() {
    // TODO Auto-generated destructor stub
}
Run Code Online (Sandbox Code Playgroud)

但是在这种情况下,QObject的前向声明会产生错误,但其余的都是okey.我需要一些帮助.

Chr*_*wet 8

这是一个纯粹的c ++"问题".

正向声明仅在编译器在包含标头时不需要类的大小时才起作用.例如,对于指针,甚至是类,它都知道大小.

另一方面,对于在堆栈上创建的对象或父类,它需要知道结构的确切大小(例如sizeof(QObject)),它只能通过包含它来获得.h.