QObjectListModel: implement 'index' method

Implementing this method allows to use QObjectListModel
in combination with other Qt classes that can act on a model
(eg: QSelectionItemModel, QSortFilterProxyModel...).
This commit is contained in:
Yann Lanthony 2024-12-06 10:12:11 +01:00
parent df4ad22b6c
commit b21192282d

View file

@ -283,6 +283,15 @@ class QObjectListModel(QtCore.QAbstractListModel):
self._objectByKey[key] = item
@QtCore.Slot(int, result=QtCore.QModelIndex)
def index(self, row: int, column: int = 0, parent=QtCore.QModelIndex()):
""" Returns the model index for the given row, column and parent index. """
if parent.isValid() or column != 0:
return QtCore.QModelIndex()
if row < 0 or row >= self.size():
return QtCore.QModelIndex()
return self.createIndex(row, column, self._objects[row])
def _dereferenceItem(self, item):
# Ask for object deletion if parented to the model
if shiboken6.isValid(item) and item.parent() == self: