The following questions are about the design of the QAbstractItemModel
and QModelIndex
classes and the interplay between them, as highlighted by the sample of code below:
class Data:
def __init__(self):
self.value = 42
class Model( QAbstractItemModel ):
def __init__( self ):
QAbstractItemModel.__init__(self)
data = Data()
modelIndex = self.createIndex( 1 , 2 , data ) ### 1
self.index( 1 , 2 , QModelIndex() ) ### 2
self.setData( modelIndex , data.value ) ### 3
self.dataChanged.emit( modelIndex , modelIndex )
modelIndex.data() ###4
self.data( modelIndex ) ### 5
- How should a
QModelIndex
be created. From my reading of the docs the answer is QAbstractItemModel::createIndex() but it seems incomplete because this function does not supply any information about the offset of the ModelIndex from its parent. Rather this is done by QAbstractItemModel::index(). Is there a way to make both functions play together? - How should data be stored in or the model index and what is the difference between the data stored by, for, or in (unsure of the terminology) the model index and the internal pointer? And where does the model index get the data that it returns when there it has no setData function? Is the internal pointer ever the data? Can it ever be the data?
- What is the difference between the data returned by the ModelIndex and the Model? i.e. QModelIndex::data() and QAbstractItemModel::data( QModelIndex , int )? And why is the setter QAbstractItemModel::setData( QModelIndex , ... ) merely virtual but the getter QAbstractItemModel::data( QModelIndex , ... ) pure virtual. Surely the API should be able to return the data that it stored.
I am aware that my question links to the C++ API whereas the snippet is in PySide. I have done so because this question cuts across both APIs.