错误:变量“QQmlComponent 组件”具有初始化程序但 Qt5 中的类型不完整

rea*_*kme 0 qt qt5

我正在根据这位导师http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.html在 Qt5 中将 C++ 类型的属性暴露给 QML 。当我运行它时,我在我的问题窗格错误中收到此错误:变量“QQmlComponent 组件”具有初始值设定项但类型完整不仅我有此错误我也有此错误未检测到我使用 Q_PROPERTY 创建的信号

C:\Users\Tekme\Documents\QtStuf\quick\QmlCpp\message.h:15: 错误:'authorChanged' 未在此范围内声明发出 authorChanged(); ^

我的代码是

#ifndef MESSAGE_H 
#define MESSAGE_H
#include <QObject>
class Message : public QObject
{
Q_OBJECT
Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
public:
    void setAuthor(const QString &a) {
        if (a != m_author) {
            m_author = a;
            emit authorChanged();
        }
    }
    QString author() const {
        return m_author;
    }
private:
    QString m_author;
};
#endif
Run Code Online (Sandbox Code Playgroud)

在我的 main.cpp 中

#include "message.h"
#include <QApplication>
#include <QQmlEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QQmlEngine engine;
    Message msg;
    engine.rootContext()->setContextProperty("msg",&msg);
    QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml"));
    component.create();

    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

thu*_*uga 5

您没有QQmlComponent在您的main.cpp:

#include <QQmlComponent>
Run Code Online (Sandbox Code Playgroud)

您还试图发出尚未声明的信号。你应该message.h像这样声明它:

signals:
    void authorChanged();
Run Code Online (Sandbox Code Playgroud)

检查这个例子