开发者

Qt error "persistent model indexes corrupted" why?

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 er开发者_如何学Pythonror 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.


QPersistentModelIndexes are (row, column, parent) references to items that are automatically updated when the referenced items are moved inside the model, unlike regular QModelIndex.
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() and endInsertRows() 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

I found this method QAbstractItemModel::persistentIndexList and I'm wondering what indexes it should return. All of them ?
Should this method return all nodes currently visible in the TreeView ?

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 a QList<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 (see QTreeWidget internal model : QTreeModel::ensureSorted(link)), for smaller incremental changes you have all the beginXxxRows/beginXxxColumns and endXxxRows/endXxxColumns methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜