Important: Please read the Qt Code of Conduct -

Problem with emitting NOTIFY signals of Q_PROPERTY

  • I am a beginner in Qt Quick. I am using Q_PROPERTY to bind a C++ member variable to a qml property. And I am using a C++ function call from qml. The C++ function emits the Notify signal. But the Qml property is not updating its value. Please help.
    My backend C++ header file code is:

    #ifndef BACKEND_H
    #define BACKEND_H
    #include <QObject>
    class Backend:public QObject
        Q_PROPERTY(QString test READ test WRITE setTest NOTIFY testChanged)
        Backend(QObject *parent=nullptr);
        void setTest(QString &temp)
            qWarning("Text Changed");
            emit testChanged();
       Q_INVOKABLE void buttonClicked()
            emit testChanged();
        void testChanged();
    public slots:
        QString test()
            return m_test;
        QString m_test;
    #endif // BACKEND_H
    My backend C++ file is:

    #include "backend.h"

    Backend::Backend(QObject *parent):QObject(parent)
    m_test="Hello world";


    My main code is:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <backend.h>
    #include <QQmlContext>
    #include <QQuickView>
    int main(int argc, char *argv[])
    QGuiApplication app(argc, argv);
    qmlRegisterType<BackEnd>("io.qt.examples.backend", 1, 0, "BackEnd");
    QQmlApplicationEngine engine;
    BackEnd data;
    // engine.setSource(QUrl("qrc:/main.qml"));

    // QQuickView view;
    // BackEnd data;
    // view.rootContext()->setContextProperty("applicationData", &data);
    // view.setSource(QUrl("qrc:/main.qml"));
    return app.exec();

    My QML code is:

    //your code here

    import QtQuick 2.6
    import QtQuick.Controls 2.0
    import io.backend 1.0
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        MouseArea {
            anchors.fill: parent
            onClicked: applicationData.buttonClicked()
        TextField {
                id: textField
                x: 198
                y: 87

  • Moderators

    You are modifying text in one object, but reading it from another.

    Here you modify applicationData - which is defined in your main.cpp as reference to data variable.

    onClicked: applicationData.buttonClicked()

    However, then you read the test property value from a different object, backend which is instantiated inside QML.


    These 2 objects are 2 completely separate instances of BackEnd class. They have different values and work independently. You need to decide which one you want to use and stick to that (so, either use only backend in your QML, or only applicationData).

  • Thank you very much for the help. It works now

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    On a side note, your implementation is suboptimal. You should rather do something like:

    void setTest(const QString &temp)
        if (m_text == temp) {
        m_test = temp;
        qWarning("Text Changed");
        emit testChanged();
    Q_INVOKABLE void buttonClicked()

    So you really emit testChanged when something has changed.

  • @SGaist Thank you for remark..........

Log in to reply