2013-08-21 25 views
1

我有一個從QAbstractListModel分類的模型,它有一個QList來維護包含QDateTime的數據,該數據用於維護此列表。我必須將這些數據保留一小時,即舊數據將從列表中刪除。這基本上是FIFO列表。我有一個代理模型(QSortFilterProxyModel的子類)來對數據進行排序。只要數據發生變化,代理模型就會丟失索引並顯示未過濾的數據。以下是執行此操作的代碼片段。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

對不起,長期以來的問題。有人可以幫助解決這個問題嗎?

謝謝。

+0

你似乎有一個樹模型。我認爲你需要爲beginInsertRows提供一個有效的父索引。或者至少這是一個猜測。 – user2672165

+0

你能告訴我們你的QAbstractListModel和QSortFilterProxyModel的子類嗎?或者至少是違規的方法? –

+0

@ user2672165我正在使用'QAbstractListModel'作爲基礎模型。 – ramtheconqueror

回答

0

beginInsertRows的文件說,void QAbstractItemModel::beginInsertRows(const QModelIndex & parent, int first, int last)這意味着當你插入只有一個項目參數第一=最後= 0的片段插入一個項目與m_entries.prepend(e)但你delcare,你要插入兩個:beginInsertRows(QModelIndex(), 0, 1);視圖接收表明兩行已經插入,並要求第二行 - 繁榮!訪問衝突。你需要的是beginInsertRows(QModelIndex(), 0, 0);。另外我不認爲你需要emit layoutAboutToBeChanged()emit layoutChanged();,但我不確定。