Solved QML custom notify event
-
Hello,
I have a QObject with 2 properties :
Q_PROPERTY(int isInitialized READ isInitialized WRITE setInitialized NOTIFY initChanged) Q_PROPERTY(QString message READ getMessage WRITE setMessage NOTIFY messageChanged)
I set this object to my QML like this :
mQuickWidget->engine()->rootContext()->setContextProperty("splash", &mSplash);
And then i my QML i use the properties like this :
Item { MouseArea { anchors.fill: parent onClicked: { splash.isInitialized= 1 splash.message="test"; } } }
This part is working great. I can send value from C++ to QML by using my mSplash object or i can edit my mSplash object from QML by clicking.
I would like in my QML to be able to "override" the initChanged NOTIFY to be able to do a specific action according to the value.
I have tried something likeComponent.onCompleted: { splash.onInitChanged = function(){ if(splash.isInitialized === 2) { //Do something here } } }
But it give me a TypeError : Cannot assign to read-only property.
I'm a complete beginner with QT et QML , so i probably missing something obvious here.
Thanks
-
Hi,
Are you looking for the connect statement ?
-
Thanks , I finally found a solution which use a Connections object :
Connections { target: splash onInitChanged : { if(splash.isInitialized === 2) { //Do something } } }
It seems that the connect statement you are talking about is kind of the same thing right ? Should i use one more than an other ?
By doing :
Component.onCompleted: { splash.onInitChanged.connect(doStuff); } function doStuff() { if(splash.isInitialized === 2) { //Do something } }
I have the exact same result , so i don't really know which solution to choose :)
-
I'd say it depends whether you'll reuse doStuff elsewhere. Otherwise, coding style and personnel taste.