[PyQt] Model/View with multiple thread

Matt Newell newellm at blur.com
Fri Feb 8 18:14:17 GMT 2008


On Thursday 07 February 2008 23:49:26 Sebastian Vetter wrote:
> Hallo everyone,
>
> I've started working with the Model/View concept and have some questions
> on implementing it in a multithreading environment. I hope that some of
> you have some ideas or advice on that and I'll highly appreciate any!
>
> So that's the basic structure: I'm working on an application that's
> processing data in a spreadsheet class. All processing in done in a
> separate thread and communicates with the GUI thread through multiple
> signals and slots. I now implemented a model in the GUI thread handling
> the exact same spreadsheet object, since the Model and View have to live
> in the same thread (is that right?).

Yes, you really want to keep all usage and updating of your model in the same 
thread as the view(s).

>
> Thinking about how to keep the spreadsheet objects in worker and main
> thread synchronised I came up with two ideas:
>
> 1. Using signals and slots in both the worker and the model to update
> their spreadsheet objects whenever the other one changes.
>
> 2. Using a thread-safe singleton of the model to handle one single
> instance of the spreadsheet that are used in both threads.
>
> What would be considered a 'better' solution? Is there any other
> alternative that might work even better?
>
> I really appreciate any thoughts on this. Thanks in advance.
>

The easiest way to update your model would be to use Queued signals from the 
worker thread to the gui thread(Queued is the default when connecting an 
object's signal to another object's slot that lives in a different thread).  
An alternative would be to use custom QEvents delivered to the model via the 
gui thread's event queue.

As for delivering updates from the model back to the work, you are probably 
best off using the same methods as above.  

Matt





More information about the PyQt mailing list