Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. QAbstractItemModel.parent index.internalPointer returning wrong object
Qt 6.11 is out! See what's new in the release blog

QAbstractItemModel.parent index.internalPointer returning wrong object

Scheduled Pinned Locked Moved Unsolved Language Bindings
1 Posts 1 Posters 709 Views
  • 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.
  • E Offline
    E Offline
    elveatles
    wrote on last edited by elveatles
    #1

    I'm not sure if this is a PySide bug or if I'm doing something wrong.
    I have subclasses a QAbstractItemModel to use with a QTreeView. When I try to refresh the children on an item, I get errors that don't make sense and sometimes crashes.

    Here is my override parent method:

    def parent(self, index):
            if not index.isValid():
                return QModelIndex()
            
            childItem = index.internalPointer()
            parentItem = childItem.parentItem
            
            if parentItem == self._rootItem:
                return QModelIndex()
            
            return self.createIndex(parentItem.row, 0, parentItem)
    

    Here is my refresh method:

    def refreshChildren(self, index):
            item = index.internalPointer()
            
            self.beginRemoveRows(index, 0, item.childCount() - 1)
            item.clear()
            self.endRemoveRows()
            
            self.beginInsertRows(index, 0, item.childCountAfterRefresh - 1)
            item.addChildren()
            self.endInsertRows()
    

    I call refreshChildren from setData which looks like this:

    def setData(self, index, value, role=Qt.EditRole):
            if not index.isValid():
                return False
            
            item = index.internalPointer()
            
            if role == Qt.EditRole:
                item.setData(index.column(), value)
    
                if index.column() == versionColumn:
                    self.refreshChildren(index)
                
                return True
            else:
                return False
    

    I always get an error in the parent method on the line parentItem = childItem.parentItem because parentItem isn't the custom item object that I created. It's another one of the objects inside of the item object. But this error only happens after I refresh. Thank you for any help.

    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