从非 UI QThread 修改 QStandardItemModel?
我有 Qt4 应用程序,它将 QStandardItemModel 绑定到 QListView 并从后台/非 UI 线程更新模型。
有时,当从非 UI 线程中反复调用 QStandardItem
的 setText(..)
方法时,应用程序会在 dataChanged(..) 处崩溃处理程序。我可以通过在 for 循环中重复调用 setText("xxxxx")
来重现该问题。在我的应用程序中,数据是从网络读取的,因此我在单独的非 UI 线程中更新模型。
这是普通的pb吗?如果我理解正确,这与排队连接有关,并且 QStandardItemModel 不是线程安全的?如何解决这个问题?
谢谢!
I have Qt4 app which binds QStandardItemModel
to the QListView
and have the model updated from background/non-UI thread.
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.
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?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.