QT - QTableView removeRow() crashing

QT - QTableView removeRow() crashing

本文关键字:crashing removeRow QTableView QT      更新时间:2023-10-16

这个函数应该从附加到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)是有效的。那么,会是什么呢?

modListtableModel之间不存在相关性。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,但该项目在其他地方被引用。这不能从代码中看出。