我的Qt /面试申请表有问题。我使用QTreeView来显示树数据。我基于QAbstractItemModel实现了自己的模型。
我在应用程序崩溃之前收到以下错误。它经常在我添加新记录后发生。
你能解释一下这个错误是什么意思吗?什么是QPersistentModelIndex? 我在代码中没有使用QPersistentModelIndex。
ASSERT failure in QPersistentModelIndex::~QPersistentModelIndex: "persistent model indexes corrupted"
感谢。
答案 0 :(得分:5)
QPersistentModelIndexes
是(行,列,父)对引用项在模型内移动时自动更新的项的引用,与常规QModelIndex
不同。
例如,如果插入一行,则位于插入点下方的所有现有持久性索引将使其row
属性增加1。
例如,您可能无法直接使用它们,但QTreeView
可以跟踪展开的项目和所选项目。
要更新这些持久性索引,您必须在添加新记录时围绕实际行插入调用函数QAbstractitemModel::beginInsertRows()
和endInsertRows()
。
有关详细信息,请参阅本节末尾有关子类化模型类的内容:http://doc.trolltech.com/latest/qabstractitemmodel.html#subclassing
我找到了这个方法
QAbstractItemModel::persistentIndexList
而我是 想知道应该返回哪些索引。所有这些?
此方法是否应返回TreeView中当前可见的所有节点?
该方法仅返回创建QPersistentIndexModel
但仍在范围内的索引(例如,作为局部变量,类成员或QList<QPersistentIndexModel>
)。
扩展或选定的节点当前不一定是可见的,因此您不能(也不应该)假设这些持久性索引的用途。
您只需要更新它们,您只需要使用persistentIndexList
对模型进行重大更改,例如排序(请参阅QTreeWidget
内部模型:QTreeModel::ensureSorted
(link)),您拥有所有beginXxxRows/beginXxxColumns
and endXxxRows/endXxxColumns
methods的增量更改。