I do not understand when to use QAbstractItemModel::createIndex()
-
This question inspired by my answer at https://forum.qt.io/post/817523. Although I believe my answer to the OP there is correct, I (apparently like him too) do not understand when one needs to use
createIndex(), as opposed to justindex()itself.In have written proxy models where I override
map{From,To}Source(), and my code works, but it doesn't mean I understand!In, say, https://codebrowser.dev/qt6/qtbase/src/corelib/itemmodels/qidentityproxymodel.cpp.html#_ZNK19QIdentityProxyModel11mapToSourceERK11QModelIndex we see
map{From,To}Source()usingcreateIndex/createSourceIndex()respectively.But in, say, https://stackoverflow.com/a/18130955/489865 we see those methods using
index/sourceModel()->index()instead.Could someone explain what calling
createIndex()versusindex()is all about? Preferably in terms I can understand :) -
J JonB referenced this topic on
-
In short - index() is a public function which can and should be called from outside to get a proper index for the given row, column and parent.
createIndex() is the internal function which actually creates a new QModelIndex() with the given row, column and some internal data to e.g. build up the parent-child tree structure for a QTreeView. Some models also store a pointer (or similar) to the actual data structure behind this model. E.g. the id to the database row so when calling data() one could do a lookup in a QMap by this id.
so index() normally calls createIndex() after some sanity checks (see e.g. QStandardItemModel -
In short - index() is a public function which can and should be called from outside to get a proper index for the given row, column and parent.
createIndex() is the internal function which actually creates a new QModelIndex() with the given row, column and some internal data to e.g. build up the parent-child tree structure for a QTreeView. Some models also store a pointer (or similar) to the actual data structure behind this model. E.g. the id to the database row so when calling data() one could do a lookup in a QMap by this id.
so index() normally calls createIndex() after some sanity checks (see e.g. QStandardItemModel@Christian-Ehrlicher
Fair enough. It's just that in those two links, both doing a proxy model derived from a base, one callsindex()inmap{From,To}Source()while the other callscreateIndex(). Not clear which then is the best or right one. -
The
mapToSourcein the StackOverflow answer is incorrect. Not because it callsindexbut because it returns an index from the proxy model and not from the source model.
It should besourceModel->index(...)instead ofindex(...).createIndexis potentially cheaper to call, but for that you'd have to now what should the internal pointer be, so it is not always possible to do so.indexwould set the internal pointer correctly.Call
createIndexwhen you can (or need), callindexinstead.
In a tree model the internal pointer is needed to retrieve where you are in the tree hierarchy from an index, in a flat table or list model it is not needed but might be used for optimization.