[PyQt] Threading crashes

andre hotz andre.hotz at piranha-bytes.com
Tue Apr 20 15:17:05 BST 2010

Mark Summerfield schrieb:
> On 2010-04-20, andre hotz wrote:
>> Hey,
>> I have a problem with my QThreads (running on windows). It is the first
>> time I use multithreading, so I am probably doing something wrong ;)
>> The situation is as follows: I have a QTreeWidget which works like a
>> filebrowser, listing a few thousand files. Creating
>> the treeitems from the files is pretty fast, but I also want the
>> treeWidget to display more information about the files.
>> This is taking some time depending on the quantity of the files. So what
>> I did is to first fill the treeView with all the filenames.
>> After that, I start an update-threat, which loops through all the items
>> in the treeWidget, gathers its data and fills the additional
>> columns in the treeView with the data. While the thread is updating the
>> treeWidget, the user can already work with the files and does not need
>> to wait for the update to finish.
>> This all works pretty well.
> I think you've been pretty lucky to have it work at all!
hehe, yeah, I was pretty happy when I got it working the first time ;)

> Qt does not provide any means of locking items or views. As a
> consequence, Qt does _not_ support updating models, scenes, or items in
> models, or items in widgets such as QTreeWidgetItems or
> QTableWidgetItems, etc. outside the main (GUI) thread. So any such
> update is potentially the source of a crash.
hm, yes, this might explain the crashes..

> One technique that could be used is to hold the data in a thread-safe
> data structure and read data from that into the GUI as needed (locking
> the data structure or each item read from it as needed), and updating
> the data from one or more secondary threads (locking as needed).
That sounds like it could work. Would it be possible to hold the data as 
you said, but when the update thread is finished, let it emit a signal 
to the treeWidget which then updates itself with the given data 
structure? Or does emitting signals from another thread also not work?

More information about the PyQt mailing list