Threadweaver or QThread?

Mirko Boehm mirko at kde.org
Thu Sep 27 07:56:01 BST 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

both will not work directly: Neither a QThread nor a ThreadWeaver job
can open a dialog, only the main thread can do this. Here is how you can
do it:

* make a slot in the parent widget of the dialog in the main thread that
shows the dialog.

* connect that slot to a signal send from the job, asynchronously.

* have the job emit the signal and then enter a wait condition.

* have the slot in the main widget show the dialog, then set the result
on the job. the method that sets the result can wake the wait condition
at the end.

This approach has the additional advantage that it does not interfere
with the main threads event loop, and the main window can control whether
the dialog should show up or not (imagine the main window being in a
modal dialog, for example).

If you have to perform such operations in many different ways, I suggest
a command pattern implementation.

The described approach works both from within a ThreadWeaver job (which
is a QObject), and from within a QThread::run method. Make sure to
explicitly create the connection asynchronously, or to otherwise be
sure the slot is not called from the second thread.

Cheers,

Mirko.


Andreas Pakulat wrote:
> Hi,
> 
> I'd like to get some clarification on something: I've got a task that
> should run outside the GUI thread, but I'd like this task to be able to
> open up a dialog to ask for some user information and then use that.
> 
> As far as I can see this seems to be impossible with ThreadWeaver, is
> that right? I mean I don't see a way to send an event from a Job and
> then let the job "wait" for another event coming to it (or rather wait
> for the dialog to be closed). Maybe I could use a QWaitCondition inside
> the job?
> 
> Am I better off with plain QThread here? (Which I'd like to not do if
> possible, because this means I'd have to find a way to limit the number
> of threads created, else Kdevelop might go out of control in some
> situations)
> 
> Thanks for any information.
> 
> Andreas
> 


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFG+1QBYSSaITCTnKURAvBbAJ9gz3ChZ/pKAOSQIzmRgFyppwmzwgCfZxGX
EeSqCp82dae7SyqG4nBeOKQ=
=GlCJ
-----END PGP SIGNATURE-----




More information about the kde-core-devel mailing list