When subclassing a QAbstractItemModel and re-implementing the index() method, I had been simply returning a new index each time with createIndex(). But I noticed that the index() method gets called thousands of times when the model is used in conjunction with a view, for all sorts of paint events and whatnot.
Should I instead be caching the QModelIndex object after I generate it the first time in index(), and then be returning the cached index when index() is subsequently called on the same row/col? It's not mentioned in the documentation, and it seems that indexes themselves can become invalidated under certain circumstances, so I am unsure of what to do here.
In my particular case, I'm working with Pyside6, but I imaging this could apply to any implementation of the Qt framework.
QModelIndex: see the note in the qt docs. You should also be wary of usingQPersistentModelIndex, since it carries a significant performance penalty. The bottom line is that you should do some real-world profiling and see whether there are any significant performance issues in the context in which your model is designed to be used (i.e. avoid premature optimisation).