QTreeView请求索引无效

2024-05-15 16:44:00 发布

您现在位置:Python中文网/ 问答频道 /正文

看看下面的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行不存在。你知道吗

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


Tags: selfindexmodelreturntitledefitemsitem
1条回答
网友
1楼 · 发布于 2024-05-15 16:44:00

当一个项被添加、移动、移除等时,模型所做的是验证QPersistentModelIndex是否有效,因此它调用qabstractemodel的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])

相关问题 更多 >