[Qt-interest] Free QAbstractItemView's model manually?
Andre Somers
andre at familiesomers.nl
Sat Sep 18 09:28:53 CEST 2010
Hi,
First of all, next time when you ask a new question, please create an
entirely new message instead of replying to an existing one. You're
messing up threading.
Op 18-9-2010 8:42, slayer81 at gmx.de schreef:
> In QListView it says, that the QListView does not take ownership of the data model, because model can be shared between more than one views. Therefore, when setting a new model with setModel() method, it advises us to
> free the former model memory.
>
> When deleting a QListView, do I also have to take care of freeing the memory of the model?
Yes, you do. But, because QAbstractItemModel is a QObject subclass, you
can create the model to have a parent object, so that parent object will
take care of proper destruction for you. If you created the model
without a parent, you need to destruct it yourself at an appropriate moment.
> If so, when is it save to free the model regarding the order of destructor calls to model and view?
It would be a good idea not to call delete on a model that is still set
as the model on a view, but there is no objection to deleting a view
that still has a model set. If you want to delete the model before the
view, you should probably first call QAbstractItemView::setModel(0) on
the views displaying that model, and then delete the model. If you don't
really care that much, and your model is only viewed by one view, then
you might as well make the view the parent object of your model, so it
takes care of destruction automatically.
André
More information about the Qt-interest-old
mailing list