PDA

View Full Version : processEvents() and threads



talk2amulya
18th February 2009, 12:14
Hi,

when we call processEvents(), is there any way to specify which thread's events it should process????

wysota
19th February 2009, 01:13
As the docs say:

Calling this function processes events only for the calling thread.

And don't even think about anything different as it would make sense.

talk2amulya
19th February 2009, 06:07
well, i read the docs and i read that..i was just clinging to a hope that it might be possible with maybe sm other function...

wysota
19th February 2009, 08:54
What do you need it for?

talk2amulya
19th February 2009, 09:30
the problem is that i m showing a splashscreen for my application and on that i m showing a loading icon(gif image loaded by QMovie)..after i show the splashscreen and start the movie, there is a lot of heavy processing going on in ONE function which consumes a lot of time..due to which the loading icon doesnt animate at all..the first thing i tried was put processEvents() around that function, but to no avail..next i went inside the function and put a lot of processEvents() here and there in the function..which made the icon animate intermittently but still not smoothly..

also, i m not allowed to make any changes in that function..so next i tried using a timer in the main() and call processEvents() through a slot to timeout(), but strangely that slot was called only one time until all the heavy processing was done..

then i tried calling processEvents() from a different thread, not knowing that it processes events of the current thread o nly..that didntwork either(of course)..thats why i was wondering if processEvents() or any other function can process events of a different thread..i really need to be able to solve this problem somehow..and searching all thru different apis in QT, i couldnt find an answer..do u have any possible solution to it?

thanks in advance!

jogeshwarakundi
19th February 2009, 09:40
hi correct me if i am wrong, but i understand that your problem is:

int main(void)
{
QApplication app;
//animation code
heavyProcessingFunc();
app.exec();
}

calling processEvents() with a timer started in main also does not give timer call back.
the timer of qt also works with the eventloop so till you do an app.exec() the event loop is not started and hence you wont get timercall backs.
if you are not allowed to change the heayProcessingFunc() you should definitely move it into another thread. or else i dont think we can figure out a way to do the animation smoothly!!!
but then the problem in interesting and a neat final solution would be a nice thing to see...

cheers!

talk2amulya
19th February 2009, 09:49
well, i cant move the heavy processing code to another thread cuz there are lots of gui calls in it..and a thread cant have gui calls..i think this problem can only be solved if i get to process events of main thread from another thread..for which i couldnt find an api..any other pointers?

jogeshwarakundi
19th February 2009, 09:56
well, i cant move the heavy processing code to another thread cuz there are lots of gui calls in it..and a thread cant have gui calls..i think this problem can only be solved if i get to process events of main thread from another thread..for which i couldnt find an api..any other pointers?

does this heavy processing function call the GUI elements directly? would it not be possible to, in some way, decouple the function and the GUI elements?
because, if it is not the case, we have a simple issue of a function which takes a lot of time being invoked in a thread! and obviously the thread does nothing but execute the function for a long time!!!

talk2amulya
19th February 2009, 10:14
THATS the reason why i was trying to call processEvents() from another thread, thinking it would solve my problem..but evidently it doesnt save my life..

^NyAw^
19th February 2009, 12:05
Hi,

Can you try to add "processEvents" calls into the process function? If you have a loop, you can add it to be called every loop iteration, ...

talk2amulya
19th February 2009, 12:28
well, i mentioned it before also..i have tried that and that does work..although the loading icon animates intermittently..but it definitely moves..but i've been asked to find "another" way of doing it..and now i m coming to the conclusion that perhaps it cant be done in any other way, if i m not missing anything that QT provides and i m not aware of or u guys are not aware of...

jogeshwarakundi
19th February 2009, 12:35
this had to be! managerial decisions on how the code should execute :p (not meant against anyone, but I have had a fair share of these kind of things myself!!!)...
but may be there is no way this can be achieved, unless ofcourse there is some obscure hard-to-find feature somewhere hidden in qt!!!

wysota
19th February 2009, 18:57
Have you seen this article?

http://doc.trolltech.com/qq/qq27-responsive-guis.html

talk2amulya
19th February 2009, 19:32
yes, that was the first thing i looked at and i remember it was u who wrote that article :) great methods to avoid all freezing but nth worked for me...

wysota
19th February 2009, 21:34
They will work for you if you follow them :) I'd try the step-by-step approach in your situation - enter early into the event loop with a timer running, divide your calculations into chunks and use QTime::elapsed() to make sure you don't starve the event loop.

talk2amulya
20th February 2009, 04:14
yes, if i divide my calculations into chunks, it IS solvable..but if i havent mentioned it earlier, i m not allowed to make ANY changes to the way currently all "calculations" are done..so my attempt was to fly past it and hdo it through a tread or start a timer with processEvents(), but none of them seem to work..also i mentioned earlier that if i put some calls to processEvents() inside the code of function where all "calculations" are done..the loading icon does animate! so my only concern is if i can bypass making ANY changes in the calculations function and still be able to solve the problem....i maybe m asking for too much here :)

wysota
20th February 2009, 08:05
Isn't placing processEvents() calls inside the calculation process also changing the way the calculations are done? I'm not sure why injecting processEvents() is fine and dividing the code into 10 methods is not.

talk2amulya
20th February 2009, 08:13
no, injecting processEvents() is NOT fine..thats why i m finding an alternate solution..the management is adamant at making no changes to the "calculation" function..and now the torch has been passed..i have been taken off the issue and sm QT guru would be looking after the issue..i m really looking forward to his stance on this :) thank you all for your time!

seneca
20th February 2009, 08:16
This may seem a elaborate solution just to get marketing happy, but how about spawning a mini-app in a second process just displaying the splash?

talk2amulya
20th February 2009, 08:26
wow, now thats smth that may catch their attention cuz they DO NOT wanna change ANTH in the calculation function...who knows..but thanks man, that might be a good thing to remember as solving a problem..just spawn another process :D :)

seneca
20th February 2009, 08:45
This is something I was seriously considering for long running print jobs, because for no apparent reason Qt does not allow to print a text document outside the main thread. However there is hope that at least this design flaw might get fixed somewhen:

http://labs.trolltech.com/blogs/2007/09/27/multi-threaded-text-layout-and-printing/