views:

794

answers:

3

Main (function main is there) thread of my program is reserved for non-GUI tasks. It calls a number of lengthy calculation functions. All implemented GUI's have been doing their work in a separate threads.

I'm now going to implement one more GUI using Qt. Qt documentation says all GUI related tasks should be done in main thread. In my case, inserting occasional QCoreApplication::processEvents() calls in main thread would be virtually useless due to great delays between them.

Is there any way to overcome this constraint of Qt? Is it impossible to do something non-GUI related in main thread of Qt program?

+5  A: 

No, you should be doing your calculations in a separate thread. As you already mentioned, there is a work-around available in QCoreApplication::processEvents(), but it sounds like you're unable to make that work for you.

If you don't want to have to do all the work of setting up a QThread and moving all your code, you may find that the QtConcurrent::run function is useful - it allows you to run a function asynchronously.

A few pointers: You should try and key your main (GUI) thread as light as possible. Large amounts of IO or calculations should either being asynchronously using QtConcurrent::run, or run inside a separate QThread. Depending on the complexity of your code, you may be able to get away with the QtConcurrent method.

Thomi
I dont see the big advantage of using QtConcurrent::run() instead of QThread. It is actually more work that way. Stick with QThread, its alot simpler to implement
yan bellavance
It depends on what you're doing and how your program is structured. If you need a permanent thread that must always be running then use QThread. If you need many small (often independent) tasks to run, and you don't care which ones are running at what time, then use QtConcurrent.
Thomi
Yeah I agree I might have been a little bit hard on it...like you say it depends on what you need.
yan bellavance
A: 

It's best to offload the long computations onto other threads so the main GUI thread remains responsive. The old-school uniprocessing way of doing things would be be to make sure your computations never run for too long without polling GUI event handler, but that doesn't scale to multi-cores.

Fortunately Qt has excellent threading support. In the past you'd have to roll-you-own system for e.g farming out tasks to a thread-pool using QThread, QMutex, QWaitCondition etc, but recent Qt releases have made things easier with higher level abstractions like QThreadPool, QtConcurrent::run and QFuture.

timday
A: 

I don't know how things will go if you call QApplication::exec() from another thread, which then becomes your gui thread. Just an idea.

(Let us know if it works, it'd be interesting...)

Marcus Lindblom
Why would you want to do this? If you need to do the work to spit your code up and run half of it on a different thread you may as well do it properly... or am I missing something?
Thomi
I'm not recommending, I'm just giving a tip on how to achieve precisely what the OP asked. The reason might be other libraries that require stuff done in main thread. (IIRC, OpenSG 1.x required rendering and loading to be done in same thread, so there we ended up with heavy OpenGL drawing in main thread (since we loaded all graphics in that thread). Not fun to have GUI running there as well, but that's how it went. (It's fixed in 2.x, IIRC).
Marcus Lindblom