Use correct overload for `QAbstractItemModel.createIndex`
-
This is a snippet taken out of my
QAbstractItemModel
:class EventItemModel(QAbstractItemModel): def __init__(self, events): super().__init__() self.events = events def index(self, row, column, parent=QModelIndex()): if not parent.isValid(): value = self.events[row].value print("internalPointer:", value, flush=True) return self.createIndex(row, column, value) value = parent.internalPointer() if isinstance(value, dict): key = tuple(value.keys())[row] return self.createIndex(row, column, value[key]) return self.createIndex(row, column, value[row]) def rowCount(self, parent=QModelIndex()): if not parent.isValid(): return len(self.events) print("internalId:", parent.internalId(), flush=True) if isinstance((value := parent.internalPointer()), (list, dict)): return len(value) return 0
Output:
internalPointer: 20.8.0.1377 internalId: 1621061188720 internalPointer: 1377 internalId: 1377 <<< crashed >>>
Notice how the
internalPointer
andinternalId
get set to same value when anint
is passed tocreateIndex
.This is due to the 2nd overload of
createIndex
being called internally. How can I make PySide call the correct overload? -
Hi,
Python not having the concept of pointers, what exactly are you trying to store as internal pointer ?
-
@SGaist I was originally try to store an integer value obtained from the source model directly (because I was running into refcount issues with
internalPointer
when using it with Python objects that kept getting GC'd).Honestly, QAIM has so many shenanigans, I switched to making a wrapper
TreeItem
class for the QAIM. I can't believe I have spent so much time trying to get just theindex()
andparent()
working. All this just because my source model's children don't remember their parents?