[SOLVED] Wait for other functions to end before terminating aplication
-
I have an application which have heavy processes within it. It has a function function1()
function1() contains these "heavy processes" and QEventLoop to make things synchronous and at the same time GUI will still be responsive
While function1() is still processing, user is so eager to close the application and hit the "Close window button on the top right (Windows)" to terminate the app..... then window closed
.... but after a second or two the window shows again. If function1() is not processing then window closes normally.
The question is how can I make my app wait for the function1() to end before closing? Data is so important that everything inside function1() should be terminated normally so that it won't corrupt. I tried to reimplement QCloseEvent with "event->accept()" but don't think it is right.
Any help? Thanks!
-
I think "this property":http://qt-project.org/doc/qt-4.8/qapplication.html#quitOnLastWindowClosed-prop of qApp is worth looking at in your case. It's true by default, but it should help you if you switch to false and control exiting yourself.
-
Any examples how to use that property?
I tried to add it in my QMainWindow constructor:
@setAttribute(Qt::WA_QuitOnClose);
qApp->setQuitOnLastWindowClosed(true); @...and behaviour still the same, Windows closes and reappear again. By the time I am using
@void MainWindow::closeEvent(QCloseEvent *ev)
{
if(isProcessing) //"isProcessing" is some bool used by my function1() to inform it isnt finished yet
{
qApp->quit(); //forced quit! Will take some seconds for the app to be reexecuted since I use QSharedMemory to allow only 1 app to run at a time
}
else
{
ev->accept();
}
}@to force quit my application when the function1() is still processing [will remove this when I find a solution].
Sorry but the documentation is a little "unhelpful". Thanks
-
Well, you probably should turn your function to be executed in a dedicated QThread.
You could then use a "volatile flag" (or emit a signal) that tells the function to stop its processing. The main (GUI) thread can then simply wait for the function's thread to stop, by using its wait() method:
(myFunction1Thread is a subclass of QThread)
@void MyMainWindow::closeEvent ( QCloseEvent * event ) {
myFunction1Thread->stopIt = true;
myFunction1Thread->wait();
}
@ -
Thanks vidar for the tip, I will try using QThread but with no guarantee since there are too much variables (and data) to exchange with the main thread plus function1() uses sql, xml, and network within it. I think this will help me though.
-
I think it's worth a try. If your application gets more and more complex, you might want to make your main GUI thread to contain ONLY interactions with the GUI and to put any kind of time-consuming tasks (e.g. large SQL queries etc.) into other threads.
Not working with a clean "thread architecture" will probably become hard to maintain. -
[quote author="Code_ReaQtor" date="1357987680"]Any examples how to use that property?
I tried to add it in my QMainWindow constructor:
@setAttribute(Qt::WA_QuitOnClose);
qApp->setQuitOnLastWindowClosed(true); @[/quote]true is the default, and you don't want that. You should turn that property to false. It will prevent your app from quitting when user clicks the close button.
-
Thanks sierdzio, I get it now.
I also made another method, that is using modal QProgressDialog as a Qt::Popup to prevent the user from clicking the close button when function1() is still executing. I guess this way will be "non-boring" for the user and will inform him that something "important" is happening. The time might take 1 sec minimum up to few minutes though, depending on the "weight" of the data it processes.
I also tried QThread as vidar suggested but I needed to segment my codes with a "flag check", and I guess it is impractical since my code is too long...
@/My codes/
if(flag) { //terminate
}
/another code/
if(flag) { //terminate
}
/another code/
if(flag) { //terminate
}
/another code/
if(flag) { //terminate
}
/another code blah blah/@"flag-checking" can also be changed into a function but still I need to segment my codes and insert function calls into it.
Thanks guys!