ramtheconqueror
ramtheconqueror

Reputation: 1964

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. I have to maintain this data for an hour i.e., older data will be removed from the list. This basically is FIFO list. I have a proxy model (subclass of QSortFilterProxyModel) to sort the data. 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.

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.

Sorry for the long question. Could someone please help in solving this problem?

Thanks.

Upvotes: 2

Views: 2430

Answers (1)

Uga Buga
Uga Buga

Reputation: 1784

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); 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);. Also I don't think you need to emit layoutAboutToBeChanged() an emit layoutChanged(); but I am not sure about that.

Upvotes: 1

Related Questions