Qt错误“持久模型索引损坏”为什么?
我的 Qt/面试应用程序有问题。我使用 QTreeView 来显示树数据。我基于 QAbstractItemModel 实现了自己的模型。
在应用程序崩溃之前我收到以下错误。添加新记录后经常发生这种情况。
您能给我解释一下这个错误的含义吗?什么是 QPersistentModelIndex ? 我没有在代码中使用 QPersistentModelIndex 。
ASSERT failure in QPersistentModelIndex::~QPersistentModelIndex: "persistent model indexes corrupted"
谢谢。
I've a problem with my Qt/interview application. I use QTreeView to display tree data. I implemented my own model based on QAbstractItemModel.
I get a following error prior to application crash. It happens often after I add new record.
Could You explain to me what is the meaning of this error. What is a QPersistentModelIndex ?
I'm not using QPersistentModelIndex in my code.
ASSERT failure in QPersistentModelIndex::~QPersistentModelIndex: "persistent model indexes corrupted"
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
QPersistentModelIndexes
是对项目的(行、列、父级)引用,当引用的项目在模型内移动时,这些引用会自动更新,这与常规的QModelIndex
不同。例如,如果插入一行,则位于插入点下方的所有现有持久索引的
row
属性都会增加 1。您可能不会直接使用它们,但 QTreeView 可以直接使用它们,例如跟踪展开的项目和选定的项目。
为了更新这些持久索引,您必须在添加时围绕实际行插入调用函数
QAbstractitemModel::beginInsertRows()
和endInsertRows()
新记录。有关详细信息,请参阅有关模型类子类化部分的末尾:http://doc.trolltech。 com/latest/qabstractitemmodel.html#subclassing
该方法仅返回创建了
QPersistentIndexModel
且仍在范围内的索引(例如作为局部变量、类成员或在QList
中) )。展开或选定的节点当前不一定可见,因此您不能(也不应该)假设这些持久索引的用途。
您只需保持它们更新,并且只需要使用
persistentIndexList
来对模型进行重大更改,例如排序(请参阅QTreeWidget
内部模型:QTreeModel::ensureSorted
(link)),对于较小的增量更改,您拥有所有beginXxxRows/beginXxxColumns
和endXxxRows/endXxxColumns
方法。QPersistentModelIndexes
are (row, column, parent) references to items that are automatically updated when the referenced items are moved inside the model, unlike regularQModelIndex
.For instance, if you insert one row, all existing persistent indexes positioned below the insertion point will have their
row
property incremented by one.You may not use them directly, but
QTreeView
does, to keep track of expanded items and selected items, for example.And for these persistent indexes to be updated, you have to call the functions
QAbstractitemModel::beginInsertRows()
andendInsertRows()
around the actual row insertion(s) when you add new records.See the end of the section about subclassing model classes for details: http://doc.trolltech.com/latest/qabstractitemmodel.html#subclassing
That method returns only the indexes for which a
QPersistentIndexModel
was created and is still in scope (as a local variable, a class member, or in aQList<QPersistentIndexModel>
for example).Expanded or selected nodes are not necessarily currently visible, so you can't (and shouldn't anyway) assume anything about what these persistent indexes are used for.
You just have to keep them updated, and you only need to use
persistentIndexList
for big changes in the model, like sorting (seeQTreeWidget
internal model :QTreeModel::ensureSorted
(link)), for smaller incremental changes you have all thebeginXxxRows/beginXxxColumns
andendXxxRows/endXxxColumns
methods.