Have you seen QFuture and QtConcurrent ?
This does exactly what you ask in the thread title.