[PyQt] Processing signals from a called method while the method executes

Andreas Pakulat apaku at gmx.de
Fri Jul 2 07:17:06 BST 2010


On 01.07.10 21:35:44, alan moore wrote:
> I've encountered this problem twice now in subtle ways, I think this
> is at the root of my last question about the QWebView; I've included
> sample code this time to illustrate.
> 
> In the attached script, I have a widget class and a processor class.
> The processor represents some kind of processing engine that's going
> to do a lengthy multi-part routine of some kind.  The widget class
> is the gui for the engine.
> 
> The "go" button is connected to the "process" method of the
> processor object.  The process() method emits a signal with a
> string, does process A for 10 seconds, emits a signal with a string,
> does process B for 10 seconds, and emits a final signal with a
> string.
> 
> The widget class is supposed to be picking up the signals and
> displaying the strings. So the expected output is:
> 
> <click "go">
> first message displays
> <wait 10 seconds>
> second message displays
> <wait 10 seconds>
> final message displays
> 
> The actual output is that nothing happens for 20 seconds, then all
> the text displays.  Because the widget isn't going to process any
> received signals until the called method exits.
> 
> How do I produce the expected behavior here?

Don't block the event-loop with your processor. The signals are
delivered as you expect, but the widget is not redrawn with that
message. The reason is that your processing blocks the Qt event loop and
hence no painting is done.

Possible options to not block the event loop that long would be to use a
QTimer to schedule each part of your processing after the next
event-loop run. The other option would be moving the processing into a
separate thread, you should only go this route after making familiar
with multi-threading though.

Andreas

-- 
Break into jail and claim police brutality.


More information about the PyQt mailing list