[Interest] QAbstractItemView and default QItemSelectionModel

Andreas Pakulat apaku at gmx.de
Mon Jun 25 23:41:12 CEST 2012


Hi,

On Mon, Jun 25, 2012 at 11:12 PM, Eric Clark <eclark at ara.com> wrote:

>  Hello All,****
>
> ** **
>
> I have a curious question about the default QItemSelectionModel that is
> created whenever a QAbstractItemView is created: Is this default selection
> model parented to the view? What I really want to know is, if I create a
> new QAbstractItemView, set the model on it and then create a new
> QItemSelectionModel and pass it into the view’s setSelectionModel()
> function, will the default selection model be deleted when the view is
> deleted? The documentation is a little hazy on this topic. It says that “It
> is up to the application to delete the old selection model if it is no
> longer needed…”, but that it will be deleted by its parent if it has one.
> So, I have a new view and I am changing its selection model to my own and
> the one that was created by default is no longer needed. Should I go ahead
> and delete the old default selection model, or is it safe to say it is
> parented to the view and will be deleted when the view is deleted?
>

You could just test what your version of Qt does with:

QAbstractItemView view;
QStandardItemModel model;
view.setModel(&model);
QItemSelectionModel selmodl = view.selectionModel();
if( selmodel.parent() == &view ) {
  fprintf(stderr, "selection model is parented under view\n");
} else {
  fprintf(stderr, "selection model has unknown/no parent\n");
}

Since the documentation does not state what behaviour is to be expected,
you shouldn't rely on this however. Since in this case I can't imagine any
possible bad side-effects of deleting the model even if it is parented
under the view, you could just be a little less lazy and delete the
selection-model before setting a new one. Or you could make sure the
selection model is set before the model, so that no default-selection-model
is created at all.

Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120625/62a7afb8/attachment.html>


More information about the Interest mailing list