QAIM - Can't select indexes from different model or with different parents
Unsolved
Qt for Python
-
@dataclass class EventPath: parent: AnyEvent | EventPath row: int def __post_init__(self) -> None: self.parents: list[EventPath] = [] ancestor = self.parent while isinstance(ancestor, EventPath): self.parents.insert(0, ancestor) ancestor = ancestor.parent self.event = ancestor self.depth = len(self.parents) self.has_children = self.event.id > DATA def __len__(self) -> int: if self.has_children and isinstance(self.value, (list, dict)): return len(self.value) # type: ignore return 0 @cached_property def children(self) -> list[EventPath]: return [EventPath(self, i) for i in range(len(self))] class EventItemModel(QAbstractItemModel): def __init__(self, events: Sequence[AnyEvent]) -> None: super().__init__() self.events = events self.paths = [EventPath(event, i) for i, event in enumerate(events)] def index(self, row: int, column: int, parent: QModelIndex = QModelIndex()) -> QModelIndex: if not parent.isValid(): return self.createIndex(row, column, self.paths[row]) path: EventPath = parent.internalPointer() return self.createIndex(row, column, path.children[row]) def parent(self, child: QModelIndex): path: EventPath = child.internalPointer() if child.column() > 0 or isinstance(path.parent, EventBase): return QModelIndex() return self.createIndex(path.parent.row, 0, path.parent) def rowCount(self, parent: QModelIndex = QModelIndex()) -> int: if not parent.isValid(): return len(self.events) path: EventPath = parent.internalPointer() return len(path) def columnCount(self, parent: QModelIndex = ...) -> int: return 2
For child level items, it always says Can't select indexes from different model or with different parents, when I clearly return the correct parent index.
Weird things happen when I try double clicking the children apart from the message above:
- Jumps to some random parent index much above it.
- Highlights some random index not at the same level.
Is it because of
cached_property
?I have done debugging in the call to
parent
to verify ifparent.parent
indeed points to the same memory location and it does. Theparent.parent.row
is same for all children too.