QSortFilterProxyModel崩溃的应用程序

时间:2013-08-21 18:03:53

标签: c++ qt

我有一个来自QAbstractListModel的子模型,它有一个QList来维护包含QDateTime的数据,用于维护此列表。我必须保持这个数据一小时,即旧的数据将从列表中删除。这基本上是FIFO列表。我有一个代理模型(QSortFilterProxyModel的子类)来排序数据。每当数据发生变化时,代理模型都会丢失索引并显示未过滤的数据。以下是执行此操作的代码段。

emit layoutAboutToBeChanged();
beginInsertRows(QModelIndex(), 0, 1); //we are prepending
m_entries.prepend(e);
endInsertRows();
emit layoutChanged();

这似乎解决了这个问题。但是,如果在视图(QTreeView)上选择了某些内容,那么应用程序会在一段时间后崩溃并出现大量错误消息。

QSortFilterProxyModel: index from wrong model passed to mapFromSource 
QSortFilterProxyModel: index from wrong model passed to mapFromSource 
QSortFilterProxyModel: index from wrong model passed to mapFromSource

调试器上的堆栈跟踪显示mouseSelectEvent和其他需要QModelIndex的函数。

很抱歉这个问题很长。有人可以帮忙解决这个问题吗?

感谢。

1 个答案:

答案 0 :(得分:0)

beginInsertRows的文档说void QAbstractItemModel::beginInsertRows(const QModelIndex & parent, int first, int last),这意味着当你只插入一个参数first = last = 0时。在你的代码片段中插入一个带有m_entries.prepend(e)的项目,但是你可能会认为你要去插入两个:beginInsertRows(QModelIndex(), 0, 1);视图接收到已插入两行的信号以及何时请求第二行 - 繁荣!访问违规。你需要的是beginInsertRows(QModelIndex(), 0, 0);。此外,我认为您不需要emit layoutAboutToBeChanged()emit layoutChanged();,但我不确定。