[Qt-interest] Handle object deletions right with containers (not-working stripped example)

Bo Thorsen bo at fioniasoftware.dk
Mon Aug 8 13:46:09 CEST 2011


First of all, you are correct that you have to call beginRemoveElement 
before deleting your object, unless you work around this. You could for 
example mark an object being deleted as unavailable, so if one of the 
views or proxies call data() or something else, you just return default 
values for it. That's a workaround that often works with these pointer 
based models.

To me this looks like an example where I would not use a signal slot 
connection for the deletes. Instead, as the first thing you do in the 
destructor of the contained object, call the deletingObject(this) on the 
model and do the removes.

Both of these approaches are valid. If you have a tree of contained 
objects, I'd go with the first of them. If you have only a single 
contained class, I'd choose the second.

Bo.

Den 08-08-2011 08:14, Wilhelm skrev:
> Hi,
>
> the attached example is motivated by working with QAbstractItemModel /
> QProxyModel (writing a proxy model that can combine other models) but
> not limited to.
>
> In the example class Container holds pointers to objects, but does not
> own(!) these objects. Any removal (and addition) of pointers results in
> sending signals beginRemoveElement() / endRemoveElement() (as in
> QAbstractItemModel).
>
> The problem arises if one of the objects are deleted without removing it
> first. I want to make this situation safe because the Container does not
> own the objects. I use the QObject::destroyed() signal connected to slot
> Container::elementDeleted(). But clearly this results in a crash calling
> Container::remove().
>
> So, how to handle this situation (bearing in mind that I'm looking for a
> solution with QAbstractItemModel, where I have a similar situation
> calling beginRemoveRows() / endRemoveRows())?
>
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest


Bo Thorsen,
Fionia Software.

-- 

Expert Qt and C++ developer for hire
Contact me if you need expert Qt help
http://www.fioniasoftware.dk



More information about the Qt-interest-old mailing list