首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:变量“QQmlComponent component”在Qt5中有初始化程序,但类型不完整

错误:变量“QQmlComponent component”在Qt5中有初始化程序,但类型不完整
EN

Stack Overflow用户
提问于 2013-10-09 11:49:21
回答 2查看 3.1K关注 0票数 0

我正在玩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();^

我的代码是

代码语言:javascript
复制
#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里

代码语言:javascript
复制
#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();
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-09 12:18:26

在您的QQmlComponent中不包括main.cpp

代码语言:javascript
复制
#include <QQmlComponent>

你也在试图发出一个你还没有声明的信号。您应该在message.h中这样声明它:

代码语言:javascript
复制
signals:
    void authorChanged();

检查这个例子

票数 5
EN

Stack Overflow用户

发布于 2013-10-09 12:27:48

我认为你需要补充:

代码语言:javascript
复制
signals: 
  void authorChanged();

像这样对你的同学说:

代码语言:javascript
复制
  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;
};
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19271200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档