[Interest] QAbstractItemView and default QItemSelectionModel

Eric Clark eclark at ara.com
Mon Jun 25 23:59:07 CEST 2012


Thanks Andreas! I know it doesn't exactly answer my question, but you are right, I can easily check this and I probably should just do the right thing anyway and delete it. However, this suggestion:

Or you could make sure the selection model is set before the model, so that no default-selection-model is created at all

I am curious about this because according to the Qt documentation on QAbstractItemView::setSelectionModel this would not work. Maybe I am misreading what it says though. Here is what it reads:

Note that, if you call setModel<http://doc.trolltech.com/4.7/qabstractitemview.html#setModel>() after this function, the given selectionModel will be replaced by one created by the view.

Do you think I am not reading this incorrectly?

Thanks You!
Eric

From: Andreas Pakulat [mailto:apaku at gmx.de]
Sent: Monday, June 25, 2012 4:41 PM
To: Eric Clark
Cc: Qt Interest (interest at qt-project.org)
Subject: Re: [Interest] QAbstractItemView and default QItemSelectionModel

Hi,
On Mon, Jun 25, 2012 at 11:12 PM, Eric Clark <eclark at ara.com<mailto: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/153ddc37/attachment.html>


More information about the Interest mailing list