[Qt-interest] Is QAbstractItemModel thread safe?

Arnold Krille arnold at arnoldarts.de
Tue Jun 30 23:26:52 CEST 2009


On Tuesday 30 June 2009 22:29:43 Ben Swerts wrote:
> > On Monday 29 June 2009 22:03:25 Eric Clark wrote:
> > > I was just wondering if it is safe to call functions in the
> > > QAbstractItemModel class from a thread that is different than the main
> > GUI
> > > thread?
> > The real question is whether you can call functions from
> > QAbstractItemModel
> > from any thread != QAbstractItemModel::thread() :-)
> > And at least from expierience I can say that it is. I have several models
> > in
> > their own thread and access each of them with two views (naturally in the
> > gui-
> > thread) and this runs well for hours (in the range of 4-7 days:).
> > The docs say that it is at least re-entreant. And I think the
> > communication
> > with the views is mostly done with signals/slots and these are sent as
> > events
> > when crossing threads. BTW: You have to do Q_DECLARE_METATYPE(
> > QModelIndex )...
> And I was just going to reply that it can be safe as long as you don't put
> a view on it...
> QAbstractItemModel notifies a QAbstractItemView by signals, so that's
> thread-safe. The view, however, then queries the model by calling its
> flags(), parent(), rowCount(), columnCount(), data(), etc. functions
> directly. I don't see how that can be thread-safe.

It is not really thread-safe. But they are all re-entrant! And as they don't 
modify the data (or modify the by just one call) there is no reason why it 
shouldn't work.

> Those functions do not change the model of course. So it could remain safe
> as long as you never change the model when any view is connected. This also
> means editable views are out of the question.

My models change constantly when views are attached. Several times a second 
for several days runtime. But I have to admit that the changes come from the 
same thread as the model and are not done via gui.

> In summary, I would strongly advise against it. I hope someone proves me
> wrong as I could use the functionality too :)

Read-only _really_ does work across thread-boundaries. I know, because I do 
that since about 2 or 3 years now...

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090630/04cbbeb2/attachment.bin 


More information about the Qt-interest-old mailing list