[PyQt] Re: problem with QAbstractItemModel

Vicent Mas uvemas at gmail.com
Sat Nov 7 10:57:19 GMT 2009


On 2009-11-07 Vicent Mas <uvemas at gmail.com> said:

> On 2009-11-07 Linos <info at linos.es> said:
> > ...
> > Hi,
> > 	in the qt examples from qt 4.5 documentation you can see they use this
> > in the model:
> > http://doc.trolltech.com/4.5/itemviews-simpletreemodel-treemodel-cpp.html
> >
> > QModelIndex TreeModel::index(int row, int column, const QModelIndex
> >  &parent) const
> >   {
> >       if (!hasIndex(row, column, parent))
> >           return QModelIndex();
> >
> >       TreeItem *parentItem;
> >
> >       if (!parent.isValid())
> >           parentItem = rootItem;
> >       else
> >           parentItem = static_cast<TreeItem*>(parent.internalPointer());
> >
> >       TreeItem *childItem = parentItem->child(row);
> >       if (childItem)
> >           return createIndex(row, column, childItem);
> >       else
> >           return QModelIndex();
> >   }
> 
> Thanks for this info. I simply didn't check this page because I was
>  interested in editable models not in read-only models. Its funny to see
>  that index method implementation in the read-only example is more complex
>  than in the editable one.
> 
> But still, it is nearly the same workaround provided by Baz and doesn't
>  answer my question about validity of the passed row argument.
> 
> > and this in the item class.
> > http://doc.trolltech.com/4.5/itemviews-simpletreemodel-treeitem-cpp.html
> >
> > TreeItem *TreeItem::child(int row)
> >   {
> >       return childItems.value(row);
> >   }
> >
> > but they can do this because QList .value method dont get an error if it
> > is out of bounds.
> 
> Yes but, as you can see in the in the index method provided in that
>  example, this is called *after* the row argument has been validated so it
>  has no impact in my question.
> 

OK, finally I got it. The validity of the parent model index passed to the 
QAbstractItemModel *must* be checked. It is said explicitely in the page 
referenced by Linos:

Models must implement an index() function to provide indexes for views and 
delegates to use when accessing data. Indexes are created for other components 
when they are referenced by their row and column numbers, and their parent 
model index. If an invalid model index is specified as the parent, it is up to 
the model to return an index that corresponds to a top-level item in the 
model.

Checking the parent validity in the way suggested by Balz will check 
implicitely the row and column validity.

As I said I did read the "Editable tree model example" which doesn't do that 
checking (I don't know why) but not the "Simple tree model example" (which 
does exactly that checking) so I was puzzled.

THANKS guys for your help.

Vicent
::

	Share what you know, learn what you don't

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20091107/95b4508a/attachment.bin


More information about the PyQt mailing list