4

I want to remove specific children from item, my parent item is const, ie. I can't replace it with a different parent item, I have to work on the one I have. Children items have multiple levels of children by itself. I've tried this but it doesn't work.

QStringList list; // contains list of names that should be deleted
for(int row=0; row < parent->rowCount(); ++row)
{
    QStandardItem* child = parent->child(row);
    bool found = 0;
    for(size_t i = 0; i<list.size(); ++i)
    {
        if(list[i] == child->text()) // check if child should be removed
        {
            found = 1;
            break;
        }
    }
    if(!found)
    {
        parent->removeRow(row); // this breaks child ordering for next iteration
    }
}

How do I do it correctly? Thanks in advance.

Altay Mazlum
  • 442
  • 5
  • 15

1 Answers1

3

You should not increment row when removing a row. Or if you keep incrementing it you should repair (decrease) the row count after removeRow:

parent->removeRow(row); // this breaks child ordering for next Iteration
--row;
Werner Henze
  • 16,404
  • 12
  • 44
  • 69
  • after testing, its not decrease row OR repair row count, correct answer is decrease row AND repair row count, but this helped me –  Jul 23 '15 at 10:29
  • @anub Sorry, it seems I wrote a bit misunderstandably. I adjusted my answer. I meant you should either not increment row when you removeRow. Or you can keep incrementing it, but also need to decrement it then (to revert the increment). – Werner Henze Jul 24 '15 at 08:31