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();
}

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 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.
i am new to stackoverflow, what do you mean accept the answer? your answer realy helped me and solved my problem.
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;
};

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.