[Qt-interest] QThreadPool, Qrunnable and Signals/Slots

Mihail Naydenov mlists at ymail.com
Fri Dec 18 20:38:09 CET 2009





----- Original Message ----
> From: Michael Jackson <mike.jackson at bluequartz.net>
> To: qt-interest at trolltech.com
> Sent: Fri, December 18, 2009 7:40:58 PM
> Subject: [Qt-interest] QThreadPool, Qrunnable and Signals/Slots
> 
> I would like to have a long running task put on another thread. I have done
> things like this before with Qt but seem to get mixed up with Qthread,
> Signals, Slots and Event Loops.
> 
> My basic design is to have a "WorkerTask" derive from QObject and QRunnable
> and implement the following:
>   virtual void run()
> signals:
> taskMessage(const QString &)
> taskProgress(int )
> taskFinished()
> slots:
> cancelTask()
> 
> Then in the main GUI Class I plan to hook up the task with the GUI using
> Signals/Slots
>   connect( task, SIGNAL(taskMessage(cons QString &)), this,
> SLOT(recTaskMessage(const QString &)) );
> ...  Connect the rest
> 
> QThreadPool->globalInstance()->start(task);
> 
> 
> Is this going to work? I have read through the docs on QThreadPool and can
> not get an indication if each thread in the QThreadPool runs its own event
> loop? Without an event loop the Signals/Slots will not be delivered? Is that
> correct? 
> 

As usual, the 'task' instance will (probably) be created in the gui thread - all events/slots will be handled there.
Pool threads do not have event loop - they must do their work (QRunnable's run()) and exit in order to free a thread for other runnables.
You, however, will be able to post/emit from run().

So, if you have a persistent (bg) job, you are better of with the classical qthread implementation scenario (ala Mandelbrot example)
QRunnable, on the other hand, is better suited for more like "fire and forget" jobs (and QtConcurrent::run and ::map(ed) etc are the easy wrappers for it)

MihailNaydenov

>   Should I be tackling this in another way?
> 
> Thanks for any advice/pointers
> 
> Mike Jackson
> 
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest



      



More information about the Qt-interest-old mailing list