Component.onDestruction not called when closing app (QML with C++ plugin)
-
Hi,
In my Taquin app, in branch develop here,
Component.onDestruction
is not called from Main.qml when closing the app. It is correctly called from other pages, when pulling the page from a pageStack.
Note: I use last clickable version: 6.24.1 and I didclickable update
.It seems not related to my app, because I did this simple test with clickable create:
- create the template QML app
- create the template C++ app
in both app, in Main.QML, in the page component, just add:
Component.onDestruction: { console.log("Destruction") }
When closing the app, I see the log with QML app, and I don't see it with C++ app!
Did you see this behavior? Any ideas how to solve it? or a workaround to do some actions when user close the app?
-
@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().