c++qmlqproperty

C++ method in QML never called, still works


I am new to QML and saw this code. It works fine, but what confuses me is that the method setUsername was never called in the qml file. So my question is how this Q_PROPERTY works. Does anyone have a good explaination for this?


Solution

  • Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged) This is used to make use of property like qml. So you can use like every variable as like a qml variable. QML will read value of property of with c++ function userName () with keyword . And write to with function setUserName(). Then the modification is notify by signal userNameChanged.