看看下面的MWE。

这是一个简单的QAbstractItemModel,只有一个级别,将其项目存储在列表中。我创建一个QTreeView来显示模型,并创建一个按钮来删除第二项。

from PyQt5.QtCore import QModelIndex, QAbstractItemModel, Qt
from PyQt5.QtWidgets import QTreeView, QApplication, QPushButton


class Item:
    def __init__(self, title):
        self.title = title


class TreeModel(QAbstractItemModel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._items = []  # typing.List[Item]

    def addItem(self, item: Item):
        self.beginInsertRows(QModelIndex(), len(self._items), len(self._items))
        self._items.append(item)
        self.endInsertRows()

    def removeItem(self, item: Item):
        index = self._items.index(item)
        self.beginRemoveRows(QModelIndex(), index, index)
        self._items.remove(item)
        self.endRemoveRows()

    # ----- overridden methods from QAbstractItemModel -----

    # noinspection PyMethodOverriding
    def data(self, index: QModelIndex, role):
        item = index.internalPointer()
        if role == Qt.DisplayRole:
            return item.title

    # noinspection PyMethodOverriding
    def rowCount(self, parent=QModelIndex()):
        if not parent.isValid():
            return len(self._items)
        return 0

    # noinspection PyMethodOverriding
    def columnCount(self, parent=QModelIndex()):
        return 1

    # noinspection PyMethodOverriding
    def index(self, row: int, col: int, parent=QModelIndex()):
        assert not parent.isValid()
        return self.createIndex(row, 0, self._items[row])

    def parent(self, index=QModelIndex()):
        return QModelIndex()


def removeItem():
    model.removeItem(item2)


if __name__ == '__main__':
    app = QApplication([])
    model = TreeModel()
    button = QPushButton('Delete')
    button.clicked.connect(removeItem)
    button.show()
    item1 = Item('Item 1')
    model.addItem(item1)
    item2 = Item('Item 2')
    model.addItem(item2)
    treeView = QTreeView()
    treeView.setModel(model)
    treeView.show()

    app.exec()


据我所知,我的模型的实现是正确的(尽管很基本)。特别是,它报告的行数和列数是正确的,并且永远不会为无效数据创建索引。

重现我的问题的步骤:


运行上面的代码。
在树形视图中,选择“项目2”。
按删除按钮。


在我的系统上,应用程序在beginRemoveRows()中崩溃,因为该视图请求第2行的QModelIndex。自然地,第2行不存在。

知道为什么当模型显式报告只有2行时,为什么QTreeView会认为会有3行?

最佳答案

当添加,移动,删除项目等时,模型要做的就是验证QPersistentModelIndex是否有效,因此它调用QAbstractItemModel的index()方法。在这种方法中,开发人员有责任验证行或列是否有效,并且为此模型提供您不使用的hasIndex()方法,这会导致您指出错误,因此解决方案是:

def index(self, row: int, col: int, parent=QModelIndex()):
    if not self.hasIndex(row, col, parent):
        return QModelIndex()
    assert not parent.isValid()
    return self.createIndex(row, 0, self._items[row])

关于python - QTreeView请求无效行的索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56261121/

10-14 00:49