Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Q_PROPERTY error
QtWS25 Last Chance

[solved] Q_PROPERTY error

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Miruvor
    wrote on last edited by
    #1

    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;
    }
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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;
      }
      @

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Miruvor
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved