0

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?

movildima
  • 37
  • 1
  • 9
  • 2
    I would try to call `delete modList->takeAt(i);` after I call `tableModel->removeRow(index);`. – vahancho Mar 25 '14 at 21:09
  • If it's crushes then **it is out of range**. Check what debug console says. Do `if (index>-1 && indexrowCount()){tableModel->removeRow(index);}` – mugiseyebrows Mar 25 '14 at 21:16

1 Answers1

0

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.

Martian Puss
  • 710
  • 2
  • 7
  • 16
  • ok ModList is a QList with pointers to `Addin`, a custom object I made that can't modify anything in the table. I don't know how it could affect the table. – movildima Mar 26 '14 at 17:24
  • Just tried to put `delete modList->takeAt(i);` before the cycle starts. Still crashing. – movildima Mar 26 '14 at 17:28
  • I double-checked the passed `index` and it's in range. What could it be? – movildima Mar 26 '14 at 17:34
  • Guess the item was referenced somewhere else. If you are sure the item index was within range and valid, then the crashing can be lead by delete something that is referenced by others. Why not post the call back stack while crashing so people can help you. – Martian Puss Mar 27 '14 at 01:28
  • Ok, how can I find the call back stack to post here? – movildima Mar 27 '14 at 20:52
  • Here's my project on [GitHub](https://github.com/movildima/FlameTool). I don't know what I did wrong but maybe the problem is not only in this function. – movildima Mar 27 '14 at 20:58
  • call stack should go with your ide. even in linux gdb can call the callstack. p.s. your model column count does not match... – Martian Puss Mar 28 '14 at 02:48
  • how do i do it with qt 5.2.0 and qt creator 3.0? – movildima Mar 28 '14 at 06:33
  • http://doc.qt.digia.com/qtcreator-2.3/creator-debug-mode.html. first goole result of "qt creator call stack"... – Martian Puss Mar 28 '14 at 08:19
  • hmm. its may be a strange bug in qt... when i have only one row in my table this function crashes. but when i have 2 or more rows it won't crash. well, i corrected that. i'll mark this answer as the correct one because you helped me. – movildima Mar 28 '14 at 13:56