PDA

View Full Version : Application won't quit until event loop quits



caelestis
7th February 2010, 08:24
I have webview and a function that waits until a page is loaded like this:



QEventLoop loop;
connect(this, SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));
loop.exec();
while (frame->url().toString().compare(page.toString()) != 0) loop.exec();


but if the page is never loaded, and while the event loop is still in exec(), when qApp's quit() is called, the program will hang after the window closed. Shouldn't there be a way to wait for something and allow the application to exit at the same time?

caelestis
8th February 2010, 01:23
I think this is an easy question for someone more knowledgeable to answer. There must be some quick solution that I am not seeing.

boudie
8th February 2010, 05:21
You don't have to start your own eventloop.
Just connect to the loadFinished signal and do all your testing in that slot.

caelestis
9th February 2010, 05:46
Yes, but I need to wait for the page to finish loading so it would be like a blocking function. Right now I have some functions and when I load a web page I need to wait until the program finishes loading the page before I continue to the next step. I need to wait for new pages many times, so splitting my one function that loads many pages into many functions connected to each other is infeasible.



foo () {
load page
wait for page to load
do stuff to the page
load page
wait for page to oad
do stuff
...
}

caelestis
10th February 2010, 05:25
I still do not have a fix, can someone provide a solution?

caelestis
11th February 2010, 05:24
Anyone? Even a small hint?

ChrisW67
11th February 2010, 08:21
Some untested thoughts...

You could establish a QSemaphore (http://doc.trolltech.com/latest/qsemaphore.html) as a member variable, with an initial count of 0. Create a slot to receive loadFinished() signals and execute QSemaphore::release() to release 1 resource (nothing else required). Between each request and the test (where you are looping now) use QSemaphore::acquire(1), which will block until a loadFinished() is received and a resource is released. You need to ensure a time out will apply if the page never returns.

You could, alternatively, busy wait on a boolean flag set by the loadFinished() receiving slot. Something like

m_finishedFlag = false;
... issue request
while (!m_finishedFlag)
qApp->processEvents();