Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML property binding is not getting refeshed with values set in C++



  • Hi All,
    We are facing the issue in the QML integration with C++. The value set using set method in C++ is not being retrieved in QML. It always shows the wrong value. Please suggest how to retrieve the value in QML using property binding.





  • @Mamatha Hi @Mamatha, you have to give more information about the way you are updating the value on C++ side and how you access the value on QML side.

    The standart/normal way to do is to define a property on C++ side, like this:

    class MyObject: public QObject
    {
        Q_OBJECT
    
        PROPERTY(QString myMessage GET getMyMessage NOTIFY myMessageChanged)
    
    public
        MyObject(QObject *parent = 0): QObject(parent), m_myMessage(QString()) {}
    
        QString getMyMessage() const { return m_myMessage }
        void updateMessage(const QString &message)
        {
            if(message != m_myMessage)
            {
                m_myMessage = message;
                emit myMessageChanged(message);
            }
        }
    
    public slots:
    
    signals:
         void myMessageChanged(const QString &message);
    
    private:
         QString m_myMessage;
    };
    

    Then on QML side, supposing you have exposed a variable called cppObject:

    Label {
    ...
        text: cppObject.myMessage 
    }
    

    That's it!


Log in to reply