Component.onDestruction not called when closing app (QML with C++ plugin)
-
@aloys, yes confirmed. You can work around with the Qt.application.active signal.
Example here: https://github.com/ubports/dialer-app/blob/xenial/src/qml/dialer-app.qml#L32And the signal handler: https://github.com/ubports/dialer-app/blob/xenial/src/qml/dialer-app.qml#L65
-
Please note that the
Qt.application.active
signal is also emitted when the application simply goes to the foreground. If you need to save some data, it's indeed a good idea to do it at that time, since you don't know whether the application will later be killed.But if you really need to know when the application is being closed, then
Qt.application
has anaboutToQuit()
signal which you can listen to via aConnections
element. -
Thank you @lduboeuf and @mardy , I use the Qt.application.active signal to fix my issue (here). What I need is to save data, so this method is appropriate for my app.
Note that when launching app on desktop (with clickable desktop), closing the app with the cross button (at upper right) will not generate this signal. But it seems not a problem at all on the device because to close the app, user need to "slide and throw" which cause a switch in an inactive state.
This should allow me to release new version of my Taquin app!
-
@aloys Maybe you should also call your function in
aboutToQuit
so that you're sure it'll be triggered. Take note that an app can be closed in different ways on Lomiri - from app spread, from launcher, from close button when in windowed mode, from crashing, from out of memory. -
Thanks @kugiigi , you're right, I will do it, this will be more robust.
And that's so simple! it works by just adding:
Connections { target: Qt.application onAboutToQuit: { console.log("About to Quit") } }
But finding the solution is not simple!
-
In fact, not so simple! I just discover another unexpected behavior.
I use Settings to save status of the game, and when adding saving on signal AboutToQuit(), the file (taquin.aloysliska.conf) is not updated.See below a simplified example from my app:
Settings { id: settings property int savedmovcount: 0 } ... Connections { target: Qt.application onAboutToQuit: { settings.savedmovcount = movcount // save movcount in settings console.log("savedmovcount", settings.savedmovcount) // re-read and display saved value } }
I see in the log that settings.savedmovcount has the expected value, but the file was not updated.
-
@aloys You could save the var in Settings when it changes
-
@cibersheep I will probably do as you said in a next release (I release recently with saving only on application inactive).
I did not do it it initially because it seems for me more proper to not cause file writing on each tile move done by the user. When playing, we can easily have hundreds of move, so hundred time of file writing (in fact i do not know how the os manage this, with cache or direct flash writing?...).Considering the problems I have encountered, this would be much simpler ... if i do not find another unexpected behavior!!! I was a bit discouraged with all these problems and the time spent on them, but let see the positive point, I have learned a lot.
Thank you all for your help!
-
@aloys said in Component.onDestruction not called when closing app (QML with C++ plugin):
In fact, not so simple! I just discover another unexpected behavior.
I use Settings to save status of the game, and when adding saving on signal AboutToQuit(), the file (taquin.aloysliska.conf) is not updated.Try also calling
settings.sync()
, that should help. -
@mardy I did not success to make it work, I have error:
qrc:/Main.qml:85: TypeError: Property 'sync' of object QQmlSettings_QML_16(0x13ee5d0) is not a function
I may miss something! I have id of Settings named appSettings, then I call appSettings.sync().