我正在使用基于此导师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
和我的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();
}
答案 0 :(得分:3)
您未在QQmlComponent
中添加main.cpp
标题:
#include <QQmlComponent>
您还试图发出尚未声明的信号。你应该在你的message.h
中声明它:
signals:
void authorChanged();
检查this example。
答案 1 :(得分:2)
我相信你需要添加:
signals:
void authorChanged();
这样对你的班级:
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;
}
signals:
void authorChanged();
private:
QString m_author;
};