[solved] Q_PROPERTY error



  • Hi,
    I just a newbie in Qt. So, maybe solution for my problem is clear and simple, but I am not able to find it.
    I have a separate header and cpp -files ( Codes below). Why compiler gives '...\message.cpp:11: error: 'textChanged' was not declared in this scope' error? As you can propably see, this is a one of the code examples found here. Just made little changes.

    message.h
    @
    #ifndef MESSAGE_H
    #define MESSAGE_H
    #include <QGuiApplication>
    #include <QQuickView>
    #include <QString>

    class Message: public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)

    public:
    Message();
    void setText(const QString &);
    QString text() const;

    private:
    QString m_text;
    };

    #endif // MESSAGE_H@

    message.cpp
    @#include "message.h"
    using namespace std;
    Message::Message()
    {
    m_text ="";
    }
    void Message::setText(const QString &Text)
    {
    if (Text != m_text) {
    m_text = Text;
    emit textChanged();
    }
    }
    QString Message::text() const
    {
    return m_text;
    }
    @


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You have not declared the textChanged signal (nor made setText a slot for that matter)

    Your class should rather look like this:
    @
    class Message: public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)

    public:
    Message();
    QString text() const;

    signals:
    void textChanged(const QString &text)

    public slots:
    void setText(const QString &text);

    private:
    QString m_text;
    }
    @



  • Thanks GSaist,
    now code compiles and I'm able to continue my tutorial tour into the secrets of the Qt.

    I took that code example from "here...":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.html
    @
    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;
    };@

    So it seems that example were a little faulty.

    Cheers!


  • Lifetime Qt Champion

    You're welcome !

    Indeed, the documentation's example might need an update. You can check the "bug report system":https://bugreports.qt-project.org/issues/ to see whether there's already something for this. If not you could open a new report.

    If it's all good for you now, can you update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.