0

i am playing with Exposing Attributes of C++ Types to QML in Qt5 based on this tutor http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.html. when i run it i got this error on my issues pane error: variable 'QQmlComponent component' has initializer but incomplete type not only i have this error i have also this error the signal i have created using Q_PROPERTY is not detected

C:\Users\Tekme\Documents\QtStuf\quick\QmlCpp\message.h:15: error: 'authorChanged' was not declared in this scope emit authorChanged(); ^

my code is

#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

and in my 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();
}
realtekme
  • 353
  • 4
  • 6
  • 16

2 Answers2

5

You are not including QQmlComponent header in your main.cpp:

#include <QQmlComponent>

You are also trying to emit a signal that you haven't declared yet. You should declare it in your message.h like this:

signals:
    void authorChanged();

Check this example.

thuga
  • 12,601
  • 42
  • 52
  • thankssss. all my errors goes away thank you very much. the qt documentation is great but for beginners like me it is a little bit difficult. – realtekme Oct 09 '13 at 13:01
  • 1
    @realtekme I do think that the tutorial you were following needs to be fixed. It has an example of a class that is missing a signal declaration. – thuga Oct 09 '13 at 13:06
  • i am new to stackoverflow, what do you mean accept the answer? your answer realy helped me and solved my problem. – realtekme Oct 10 '13 at 06:49
2

I believe you need to add:

signals: 
  void authorChanged();

to your class like this:

  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;
};
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • Hmm. I swear the other answer was not on my browser when I started answering. Although I did get a notification that an answer was posted after I had half finished. – drescherjm Oct 09 '13 at 12:29