GUI (PyQt) & thread problem

Jason Orendorff jason at jorendorff.com
Mon Feb 25 10:50:57 CET 2002


> Thank you for your reply. Do you have any pointers on how to implement
> this kind of functionality, i.e. when something happens in the worker
> thread (not GUI related), the GUI needs to be updated. Is it at all
> possible?

Yes.  There are two things you can try.  Both are documented for
C++ Qt here, briefly:
  http://doc.trolltech.com/3.0/threads.html


1.  When something happens in the worker thread, post an event
    about it.  A moment later, the GUI thread will pick up the
    event and handle it as normal.

This is the preferred technique and you can use it to post either
a normal basic event like a QPaintEvent (to cause a simple repaint)
or to post a special magic event of your own to any QObject of your
choosing.  You can be sure that when the target object
receives the event, it is in the GUI thread and can call GUI
functions.

The method you need for this is QThread.postEvent().
I can't tell if this function is available from PyQt or not.

2.  When something happens in the worker thread, have the
    worker acquire the Qt library mutex.  When you do this,
    your worker thread then may call whatever GUI functions
    it likes.  It then simply releases the mutex to turn control
    back over to the normal GUI thread.

Only do this if #1 doesn't work.

## Jason Orendorff    http://www.jorendorff.com/





More information about the Python-list mailing list