OK I think I'm getting closer to understanding: thanks.
Note just in case anyone comes here wondering what the heck I'm going on about, below I paste the main discussion of "root items" I found in the docs accompanying simpletreemodel and editabletreemodel. In particular, note the claim therein "The root item corresponds to the null model index, QModelIndex()" which is what had me going down the wrong path:
Root item (the TreeItem in the data structure)
The root item in the tree structure has no parent item and it is never referenced outside the model. Although the root item (with no parent item) is automatically assigned a row number of 0, this information is never used by the model.
Note that, since the root item in the model will not have a parent, TreeItem.parent() will return 0 [in Python, None] in that case. We need to ensure that the model handles this case correctly when we implement the TreeModel.parent() function.[T]op-level items...can be obtained from the root item by calling its child() function, and each of these items return the root node from their parent() functions
Root item's "index" (handling of rootItem in TreeModel)
We place an item at the root of the tree of items. This root item corresponds to the null model index, QModelIndex(), that is used to represent the parent of a top-level item when handling model indexes. Although the root item does not have a visible representation in any of the standard views, we use its internal list of QVariant objects to store a list of strings that will be passed to views for use as horizontal header titles.
It is up to the TreeModel constructor to create a root item for the model. This item only contains vertical header data for convenience. We also use it to reference the internal data structure that contains the model data, and it is used to represent an imaginary parent of top-level items in the model.
The TreeModel destructor ensures that the root item and all of its descendants are deleted when the model is destroyed. It only has to delete the root item; all child items will be recursively deleted by the TreeItem destructor.
Items without parents, including the root item, are handled by returning a null model index. We only need to ensure that we never return a model index corresponding to the root item. To be consistent with the way that the index() function is implemented, we return an invalid model index for the parent of any top-level items in the model [including rootItem].
The seeming contradiction between the claims 1) "We...need to ensure that we never return a model index corresponding to the root item" and 2) "The root item corresponds to the null model index, QModelIndex()" was just one of the things that had (has) me confused.
Added after 17 minutes:
I just noticed that QTreeView has a function setRootIndex(). It is defined, in qtreeview.cpp with:
{
d->header->setRootIndex(index);
}
void QTreeView::setRootIndex(const QModelIndex &index)
{
Q_D(QTreeView);
d->header->setRootIndex(index);
QAbstractItemView::setRootIndex(index);
}
To copy to clipboard, switch view to plain text mode
And in qabstractitemview.cpp we have:
/*!
Sets the root item to the item at the given index.
*/
{
if (index.isValid() && index.model() != d->model) {
qWarning("QAbstractItemView::setRootIndex failed : index must be from the currently set model");
return;
}
d->root = index;
d->doDelayedItemsLayout();
}
/*!
Sets the root item to the item at the given index.
*/
void QAbstractItemView::setRootIndex(const QModelIndex &index)
{
Q_D(QAbstractItemView);
if (index.isValid() && index.model() != d->model) {
qWarning("QAbstractItemView::setRootIndex failed : index must be from the currently set model");
return;
}
d->root = index;
d->doDelayedItemsLayout();
}
To copy to clipboard, switch view to plain text mode
This strongly suggests it is mistaken to say that the root index doesn't exist, full stop. Perhaps the thing to say is that it defaults to QModelIndex()? It actually seems there is some functionality built in that I was looking for, albeit in the view not part of QAbstractItemModel.
Note QTreeView also seems to inherit the rootIndex method:
/*!
Returns the model index of the model's root item. The root item is
the parent item to the view's toplevel items. The root can be invalid.
\sa setRootIndex()
*/
{
}
/*!
Returns the model index of the model's root item. The root item is
the parent item to the view's toplevel items. The root can be invalid.
\sa setRootIndex()
*/
QModelIndex QAbstractItemView::rootIndex() const
{
return QModelIndex(d_func()->root);
}
To copy to clipboard, switch view to plain text mode
Indeed, when I run view.rootIndex() on the view in the simpletreemodel example, I get the "invalid" index back.
Bookmarks