Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QAIM - Can't select indexes from different model or with different parents
Forum Updated to NodeBB v4.3 + New Features

QAIM - Can't select indexes from different model or with different parents

Scheduled Pinned Locked Moved Unsolved Qt for Python
qt for python
1 Posts 1 Posters 231 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    demberto
    wrote on last edited by demberto
    #1
    @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 if parent.parent indeed points to the same memory location and it does. The parent.parent.row is same for all children too.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved