简体   繁体   English

QSortFilterProxyModel崩溃的应用程序

[英]QSortFilterProxyModel crashing app

I have a model subclassed from QAbstractListModel which has a QList to maintain the data which contains QDateTime which is used to maintain this list. 我有一个从QAbstractListModel子类化的模型,该模型具有QList来维护包含QDateTime的数据,该数据用于维护此列表。 I have to maintain this data for an hour ie, older data will be removed from the list. 我必须将数据保留一个小时,即较旧的数据将从列表中删除。 This basically is FIFO list. 这基本上是FIFO列表。 I have a proxy model (subclass of QSortFilterProxyModel ) to sort the data. 我有一个代理模型( QSortFilterProxyModel子类)来对数据进行排序。 Whenever the data changes, proxy model is loosing the index and displaying data unfiltered. 每当数据更改时,代理模型就会丢失索引并显示未过滤的数据。 Following is the code snippet to do this. 以下是执行此操作的代码段。

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

This seems to have solved the problem. 这似乎已经解决了问题。 But, if something is selected on the view ( QTreeView ), then the application is crashing after sometime with lot of these error messages. 但是,如果在视图( 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

Stack trace on the debugger shows the mouseSelectEvent and other functions which needs QModelIndex . 调试器上的堆栈跟踪显示mouseSelectEvent和其他需要QModelIndex函数。

Sorry for the long question. 对不起,很长的问题。 Could someone please help in solving this problem? 有人可以帮忙解决这个问题吗?

Thanks. 谢谢。

The documentation of beginInsertRows says void QAbstractItemModel::beginInsertRows(const QModelIndex & parent, int first, int last) which means that when you insert only one item parameters first = last = 0. In your snippet you insert one item with m_entries.prepend(e) but you delcare that you are going to insert two: beginInsertRows(QModelIndex(), 0, 1); beginInsertRows的文档说void QAbstractItemModel::beginInsertRows(const QModelIndex & parent, int first, int last) ,这意味着当您仅插入一个项目参数first = last = 0时。在您的代码段中,您可以使用m_entries.prepend(e)但您担心要插入两个: beginInsertRows(QModelIndex(), 0, 1); The view receives signal that two rows have been inserted and when it asks for the second one - boom! 该视图接收到已插入两行的信号,并在请求第二行时发出信号-动臂! access violation. 访问冲突。 What you need is beginInsertRows(QModelIndex(), 0, 0); 您需要的是beginInsertRows(QModelIndex(), 0, 0); . Also I don't think you need to emit layoutAboutToBeChanged() an emit layoutChanged(); 我也不认为您需要emit layoutAboutToBeChanged()emit layoutChanged(); emit layoutAboutToBeChanged() but I am not sure about that. 但我不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM