5

我的 Qt/面试应用程序有问题。我使用 QTreeView 来显示树数据。我基于 QAbstractItemModel 实现了我自己的模型。

在应用程序崩溃之前出现以下错误。添加新记录后经常发生这种情况。

您能否向我解释一下这个错误的含义。什么是 QPersistentModelIndex ?我没有在我的代码中使用 QPersistentModelIndex。

ASSERT failure in QPersistentModelIndex::~QPersistentModelIndex: "persistent model indexes corrupted"

谢谢。

4

1 回答 1

7

QPersistentModelIndexes是(行,列,父)对项目的引用,当被引用的项目在模型内移动时会自动更新,这与常规的QModelIndex.
例如,如果您插入一行,则位于插入点下方的所有现有持久索引的row属性都将增加 1。

例如,您可能不会直接使用它们,但可以使用它们QTreeView来跟踪扩展项目和选定项目。

对于要更新的​​这些持久索引,您必须在添加新记录时调用函数QAbstractitemModel::beginInsertRows()endInsertRows()围绕实际的行插入。

有关详细信息,请参阅关于子类化模型类部分的末尾:http: //doc.trolltech.com/latest/qabstractitemmodel.html#subclassing

我找到了这个方法QAbstractItemModel::persistentIndexList,我想知道它应该返回什么索引。他们都是 ?
此方法是否应该返回当前在 TreeView 中可见的所有节点?

该方法仅返回为其QPersistentIndexModel创建了 a 且仍在范围内的索引(作为局部变量、类成员或在 aQList<QPersistentIndexModel>中)。

扩展或选定的节点当前不一定可见,因此您不能(也不应该)假设这些持久索引的用途。

你只需要让它们保持更新,你只需要persistentIndexList在模型中使用大的变化,比如排序(参见QTreeWidget内部模型:(QTreeModel::ensureSorted链接)),对于较小的增量变化,你有所有的beginXxxRows/beginXxxColumnsendXxxRows/endXxxColumns方法

于 2011-09-13T01:50:02.547 回答