从非UI QThread修改QStandardItemModel吗? [英] Modifying QStandardItemModel from non-UI QThread?
问题描述
我有将QStandardItemModel
绑定到QListView
的Qt4应用,并从后台/非UI线程更新了模型.
I have Qt4 app which binds QStandardItemModel
to the QListView
and have the model updated from background/non-UI thread.
有时,当从非UI线程非常重复地调用QStandardItem
的setText(..)
方法时,应用程序将在la dataChanged(..)处理程序上崩溃.我可以通过在for循环中反复调用setText("xxxxx")
来重现该问题.在我的应用中,数据是从网络读取的,因此我在单独的非UI线程中更新了模型.
Sometimes, when the QStandardItem
's setText(..)
method is called very repeatedly from the non-UI thread, the application will crash at a la dataChanged(..) handler. I can reproduce the issue by calling setText("xxxxx")
repeatedly in a for loop. In my app, the data is read from network hence I update the model in separate, non-UI thread.
这是常见的铅吗?如果我理解正确,这与排队连接有关,并且QStandardItemModel
不是线程安全的吗?如何解决这个问题?
Is this a common pb? If I understand correctly, this is related to queued connection and QStandardItemModel
is not thread safe? How to get around this issue?
谢谢!
推荐答案
QStandardItemModel
是QtGui库的一部分,iirc中的所有类均被认为不是线程安全的.如果将数据的实际设置移到GUI线程中应该没问题.您可以通过使用网络线程发出的排队信号来轻松地做到这一点.
QStandardItemModel
is part of the QtGui Library, iirc all classes in there are considered not threadsafe. You should be ok if you move the actual setting of the data into the GUI thread. You can do that pretty easily by using a queued signal from you network thread.
这篇关于从非UI QThread修改QStandardItemModel吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!