48

I have a QML based application in Qt that generates some warnings at runtime:

QDeclarativeExpression: Expression "(function $text() { return pinyin })" depends on non-NOTIFYable properties: hanzi::DictionaryEntry::pinyin

I believe it refers to this class which has some properties with no notifier (because not needed):

#ifndef DICTIONARYENTRY_H
#define DICTIONARYENTRY_H

namespace hanzi {

class DictionaryEntry : public QObject {

    Q_OBJECT

    Q_PROPERTY(QString simplified READ simplified)
    Q_PROPERTY(QString traditional READ traditional)
    Q_PROPERTY(QString pinyin READ pinyin)
    Q_PROPERTY(QString definition READ definition)

public:

    explicit DictionaryEntry(QObject* parent = 0);
    const QString& simplified() const;
    const QString& traditional() const;
    const QString& pinyin() const;
    const QString& rawDefinition() const;
    const QStringList& definitions() const;
    const QString& definition() const;
    void setSimplified(const QString& v);
    void setTraditional(const QString& v);
    void setPinyin(const QString& v);
    void setDefinitions(const QStringList& v);

};

}
#endif // DICTIONARYENTRY_H

Does anybody know why it's showing these warnings, and, if they are not important, is there any way to disable them?

Exa
  • 4,020
  • 7
  • 43
  • 60
laurent
  • 88,262
  • 77
  • 290
  • 428

1 Answers1

101

If the property values can change, then QML needs a NOTIFY signal so it can know when they have changed and update property bindings.

If they can't change, add CONSTANT to your property declaration, for example:

Q_PROPERTY(QString simplified READ simplified CONSTANT).

In your case, there are set methods, which implies the properties can change, but if they don't change when they're being used in your QML, you can get rid of the warnings by marking them as CONSTANT.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
Dan Milburn
  • 5,600
  • 1
  • 25
  • 18
  • 2
    The property I'm passing is also declared in qml. How can I make it constant? – AndroC Jun 06 '19 at 13:45
  • What about if those qt properties can change, but not on that specific QML? For example, a class called user with email, PIN, password,... fields. On login page, none of the properties can be modified, but once the user logs in, they can. Is it possible to disable these warnings for a specific QML component? can we define on QML side: "property static string password = user.password"? – laurapons Sep 04 '19 at 10:40
  • I don't think the fact that they have set methods makes a difference here. I am getting this error even without the set method. I wonder why the Qt documentation has this as an example instead: `Q_PROPERTY(bool focus READ hasFocus)` – pooya13 May 16 '21 at 04:20