Question

This function should remove a row from my QStandardItemModel attached to a QTable View.

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);
}

The strange thing is that the program crashes at the last instruction, tableModel->removeRow(index); And its not going out of range because tableModel->item(index,0) is valid. What could it be, then?

Was it helpful?

Solution

the code does not present relativity between modList and tableModel. tableModel->item(index,0) was valid before changing modList, while tableModel->rowAt(index) becomes invalid after modifying. There are a few possibilities:

Modifying modList affects tableModel, as @vahancho implies. This can be verified by commenting out the for loop or changing the order of lines. This can be lead by use modList as the real data of tableModel, for example, are you implementing a custom QTableModel by returning modList->at(i) as QTableModel::Data and returning modList->count() as QTableModel::rowCount()?

modList does not affect tableModel, but the item was referenced somewhere else. this cannot be tell from the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top