2

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?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

2 Answers2

4

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.

Mihayl
  • 3,821
  • 2
  • 13
  • 32
0

This code is an example of communication between QML (JavaScript) and C++. This code exposes the author property, so you can reach it from the JavaScript code. If you change the author property from the C++ side you have to inform the QML engine about it. The NOTIFY field of the Q_PROPERTY macro indicates a signal and when it is emitted the QML engine re-reads that property.

Message {
    id: msg
    author: "Me"        // this property was defined in c++ side with the 
                        // Q_PROPERTY macro.
}

Text {
    width: 100; height: 100
    text: msg.author    // invokes Message::author() to get this value

    Component.onCompleted: {
        msg.author = "Jonah"  // invokes Message::setAuthor()
    }
}
Broothy
  • 659
  • 5
  • 20