c++qtqmlqproperty

How are signals emitted through Q_PROPERTY caught?


http://doc.qt.io/qt-5/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;
    }
signals:
    void authorChanged();
private:
    QString m_author;
};

They have written emit authorChanged();.

I want to know where is the slot for this signal?

Which code will get changed when the authorChanged() signal is emitted?


Solution

  • If you're using this property from C++ you have to provide and connect the slots yourself, but in Qml if you read the rest:

    In the above example, the associated NOTIFY signal for the author property is authorChanged, as specified in the Q_PROPERTY() macro call. This means that whenever the signal is emitted — as it is when the author changes in Message::setAuthor() — this notifies the QML engine that any bindings involving the author property must be updated, and in turn, the engine will update the text property by calling Message::author() again.

    it says that the NOTIFY part of the macro informs the QML engine that it has to connect to this signal and update all bindings involving this property.

    The Q_PROPERTY just exposes the property but the actual works happens in setAuthor which also emits the signal. QML also uses this this method if a property is set.

    UPDATE:

    Q: I want to know where is the slot for this signal?

    The slots in QML are in the QML engine.

    Q: Which code will get changed when the authorChanged() signal is emitted?

    The QML updates all bindings involving the specified property.