I prepared a QAbstractListModel, whose model indices contain a pointer that I absolutely need to process the data.
If you start with the wrong requirements, you get the wrong decisions :)
The list model is simple enough, so you don't need more than QModelIndex row() to uniquely identify data by index addresses.
So, considering QModelIndex mi , when did you do before
PointItem * item = static_cast<PointItem*>(mi.internalPointer());
you can instead
PointItem * item = plm->pointItemFromIndex(mi);
where plm is your PointListModel . If you do not have a pointer to it, when you need to access PointItem , you can restore it as follows:
PointItemModel * plm = qobject_cast<PointItemModel*>(mi.model()); // check for !plm here (!mi.isValid() || qobject_cast fails)
In turn, PointListMode::pointItemFromIndex() will do the actual work:
PointItem * PointListMode::pointItemFromindex(const QModelIndex &mi) const { return mi.isValid() ? m_points[mi.row()] : 0 ; }
This is the most important thing to implement when working with QAbstractListModel in Qt: mentally replace QModelIndex with int row , ignore everything else that it has (invalid QModelIndex has row() == -1 ).
Same thing for QAbstractTableModel : mentally reduce QModelIndex to int row, int column . Forget the rest.
The only time you need a full QModelIndex (including its internalPointer() or internalId() ) when you implement the tree model ( QAbstractItemModel ).
Marc Mutz - mmutz
source share