Qt World Summit: Register Today!

QAbstractItemModel.parent index.internalPointer returning wrong object

  • 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)
            self.beginInsertRows(index, 0, item.childCountAfterRefresh - 1)

    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:
                return True
                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.

Log in to reply