Anyone have a good grasp of how a model should be setup so internalPointer() will work correctly? I’m doing something wrong, but not sure what…
current setup:
def MyModel(QtCore.QAbstractTableModel):
...
self._myData #Just a simple python list of some data objects, passed in
def data(self, index, role):
...
row = index.row()
return self._myData[row] # works, but... is this right? How can I set it up so if I grab index.internalPointer(), I'll actually get my data object?
i think you need to implement .createIndex() and give it your data there
from what I understand, you don’t need to do anything more (unless you are creating a tree view, which is a more complicated setup).
some signals emit the index that was modified or selected or added/removed, and when you pass that index to the slot that handles the operation, you can use index.internalPointer() to figure out what was operated on.
Yes, these are both correct answers. For a table, you generally won’t need to use internalPointer(). What you are doing in your example code is correct.
If you need to use internalPointer(), then calling createIndex() is what you need to do. The createIndex() function takes three arguments: row, column, object. The third argument, that I call “object”, can be either a unique id or an arbitrary object. When you call internalPointer() on a QModelIndex, the “object” that was assigned in the call to createIndex() is the one that will be returned.
Every index in your model must have a unique combination of (row, column, object). For tables and lists, “object” can be the same for every call to createIndex() because (row, column) is unique. (Notice that I said it can be the same. It does not have to be the same.) But for a tree, (row, column) might be found in multiple places. For example, a tree might have a node (row=1, column=0) parented to the root node. That node might also have children nodes, including one at (row=1, column=0). Each of these instances must have a unique object to point to.