我正在玩C++类型的属性向Qt5中的QML公开的游戏,基于这个导师http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.html。当我运行它时,我在我的问题窗格错误上得到了这个错误:变量'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();
}发布于 2013-10-09 12:18:26
在您的QQmlComponent中不包括main.cpp头
#include <QQmlComponent>你也在试图发出一个你还没有声明的信号。您应该在message.h中这样声明它:
signals:
void authorChanged();检查这个例子。
发布于 2013-10-09 12:27:48
我认为你需要补充:
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;
};https://stackoverflow.com/questions/19271200
复制相似问题