maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   SailfishOS (https://talk.maemo.org/forumdisplay.php?f=52)
-   -   Closing an application without killing main loop (https://talk.maemo.org/showthread.php?t=91947)

vranki 2013-12-04 17:11

Closing an application without killing main loop
 
Hi,

I'm writing an application which has to do dome network operations after user has closed the UI. Normally this is not possible, as the applications Qt main loop exits when closing the UI.

On Harmattan i found a workaround - re-starting the mainloop by calling QGuiApplication::exec(). This seems to crash on Sailfish.

The next workaround i tried to use is to create an event filter for the QGuiApplication and try to filter out the close event. QEvent::Close never happens, but QEvent::ApplicationDeactivate does. It can be used to prevent killing main loop, but unfortunately it also happens when user swipes the application to the background.

Any ideas on how can i accomplish this?

qwazix 2013-12-04 17:15

Re: Closing an application without killing main loop
 
It's not exactly what you're asking, but you could create a daemon and control it from your application via dbus, so you don't have to work around the close event.

xerxes2 2013-12-04 17:22

Re: Closing an application without killing main loop
 
I think what you want is the aboutToQuit signal which doesn't quit the mainloop until its callback returns.

http://qt-project.org/doc/qt-5.0/qtc...ml#aboutToQuit

pycage 2013-12-04 17:48

Re: Closing an application without killing main loop
 
How about

Code:

app->setQuitOnLastWindowClosed(false);
in main.cpp?

javispedro 2013-12-04 17:48

Re: Closing an application without killing main loop
 
Quote:

Originally Posted by vranki (Post 1392595)
I'm writing an application which has to do dome network operations after user has closed the UI. Normally this is not possible, as the applications Qt main loop exits when closing the UI.

Actually the reason is that closing the last window generates a quit event. See http://qt-project.org/doc/qt-4.8/qap...dowClosed-prop

vranki 2013-12-04 19:36

Re: Closing an application without killing main loop
 
Quote:

Originally Posted by pycage (Post 1392609)
Code:

app->setQuitOnLastWindowClosed(false);

Does not work - the main loop still exits. Same with Harmattan.

vranki 2013-12-04 19:47

Re: Closing an application without killing main loop
 
Quote:

Originally Posted by xerxes2 (Post 1392602)
I think what you want is the aboutToQuit signal which doesn't quit the mainloop until its callback returns.

http://qt-project.org/doc/qt-5.0/qtc...ml#aboutToQuit

I created a aboutToQuit slot like this:

Code:

// Called when app about to quit - handle upsync & quitting
void ClientLogic::aboutToQuit() {
    qDebug() << Q_FUNC_INFO;
    QEventLoop eventLoop;
    eventLoop.exec();
}

On emulator quitting with this results in output:

Code:

virtual void ClientLogic::aboutToQuit()
The Wayland connection broke. Did the Wayland compositor die?
Killing all inferiors
Debugging has finished

Debugger just displays ??'s as stack trace so it's useless. The crash happens when eventLoop.exec() is called.

coderus 2013-12-04 19:54

Re: Closing an application without killing main loop
 
why you need this? isnt better to rewrite logics?
set event filter, catch close event, do whatever.

xerxes2 2013-12-04 20:00

Re: Closing an application without killing main loop
 
Quote:

Originally Posted by vranki (Post 1392662)
I created a aboutToQuit slot like this:

Code:

// Called when app about to quit - handle upsync & quitting
void ClientLogic::aboutToQuit() {
    qDebug() << Q_FUNC_INFO;
    QEventLoop eventLoop;
    eventLoop.exec();
}

On emulator quitting with this results in output:

Code:

virtual void ClientLogic::aboutToQuit()
The Wayland connection broke. Did the Wayland compositor die?
Killing all inferiors
Debugging has finished

Debugger just displays ??'s as stack trace so it's useless. The crash happens when eventLoop.exec() is called.

Why are you calling eventLoop.exec()? You already has one mainloop running so just put your cleanup code in the aboutToQuit callback instead. The mainloop will not exit before the aboutToQuit callback returns. There was a bug in earlier sdk but in the latest version it works just fine.

http://talk.maemo.org/showthread.php...ht=abouttoquit

vranki 2013-12-04 20:05

Re: Closing an application without killing main loop
 
Quote:

Originally Posted by coderus (Post 1392666)
why you need this?

Because the application needs to synchronize it's state to a remote server when it is closed.

Quote:

Originally Posted by coderus (Post 1392666)
isnt better to rewrite logics?
set event filter, catch close event, do whatever.

I have been planning to make a separate daemon and ui, but due to limited time available i am not going to do this anytime soon.

I'd be even happy to write a patch to Qt or Sailfish itself if it is the only way and would be accepted into upstream.


All times are GMT. The time now is 18:08.

vBulletin® Version 3.8.8