QT - QTableView removeRow() crashing
QT - QTableView removeRow() crashing
这个函数应该从附加到QTable视图的QStandardItemModel中删除一行。
void ModManager::delete_Addin(int index)
{
QString addinId;
int i;
addinId = tableModel->item(index,0)->text();
for(i=0;i<modList->size();i++)
{
if(modList->at(i)->Id() == addinId)
{
delete modList->takeAt(i);
break;
}
}
tableModel->removeRow(index);
}
奇怪的是程序在执行最后一条指令tableModel->removeRow(index);
时崩溃了它不会超出范围,因为tableModel->item(index,0)
是有效的。那么,会是什么呢?
modList
与tableModel
之间不存在相关性。tableModel->item(index,0)
在修改modList
之前是有效的,而tableModel->rowAt(index)
在修改后失效。有以下几种可能性:
修改modList
会影响tableModel
,如@vahancho所示。这可以通过注释掉for循环或更改行顺序来验证。这可以通过使用modList
作为tableModel
的实际数据来实现,例如,您是否通过将modList->at(i)
返回为QTableModel::Data
并将modList->count()
返回为QTableModel::rowCount()
来实现自定义QTableModel ?
modList
不影响tableModel
,但该项目在其他地方被引用。这不能从代码中看出。