我根據文檔實現了removeRows()
方法。我的數據存儲在QList中。刪除QAbstractTableModel中的最後一項時出現「索引超出範圍」
bool MeasurementManager::removeRows(int row, int count, const QModelIndex &m) {
if(count > 1) {
qDebug() << "MeasurementManager: cannot remove more than one measurement";
return false;
}
beginRemoveRows(QModelIndex(), row, row+count-1);
list.removeAt(row);
endRemoveRows();
return true
}
但是,在執行beginRemoveRows()
當刪除最後一個項目,我得到了以下錯誤消息,當:使用我可以刪除的項目就好
ASSERT failure in QList<T>::at: "index out of range"
刪除最後一個項目時(導致碰撞),它顯然必須在0行,但只要模型中有其他項目,我可以刪除行0中的項目,而不會有任何問題。
如果我註釋掉這樣
beginRemoveRows(QModelIndex(), row, row+count-1);
//list.removeAt(row);
endRemoveRows();
實際去除我的數據不會發生崩潰,所以我的假設是,某事試圖訪問後取出列表的元素之一。但是,在逐步完成功能時,beginRemoveRows()
方法顯然是罪魁禍首。
任何幫助從哪裏開始調試,將不勝感激!