Catching close events in QML or C++
-
Is there a way to get a signal when the user closes an app? (not backgrounding, but complete app close)
Doesn't matter if its via QML or C++, both would be fine for me. -
Humm lets try the obvious child, how about the destructor of QApplication? See https://stackoverflow.com/questions/4381841/qt-how-to-intercept-the-applications-close-event-if-theres-one
-
@flohack And in the destructor of the QApplication, will the QML engine and all the stuff still exist and work?
[edit] Maybe I should have mentioned that, while C++ is an option, I need to trigger actions in QML, namely stopping
MediaPlayer
playback. -
Oh well
Maybe better then to hook https://doc.qt.io/qt-5/qquickwindow.html#closing ? Or similar, Google will help you out. -
@flohack Yeah well, as I said, I don't have a window. I have a
QQuickView
or aUbuntu.Components MainView
.
I have already googled quite a bit and tried several solutions, but none worked, thats why I ask here. -
@s710 Well they should be derived and as such have that event too. Did you try out to subscribe ?
-
@flohack Yep.
qrc:/Main.qml:20:4: Cannot assign to non-existent property "onClosing"
-
Maybe play with applicationState : see https://stackoverflow.com/questions/50845884/how-to-listen-to-qguiapplicationapplicationstatechanged-signals-directly-in-a
-
@lduboeuf This works, but it does not provide any info about the app closing. Only active/inactive/hidden/suspended.
-
Got it, after googling even more. Some guy at the QT forum revealed:
Connections { target: Qt.application onAboutToQuit: { audioPlayer.stop() }
This works