PDA

View Full Version : Crash on removing a row



inktomi
10th July 2009, 07:49
Hi,

I'm trying to remove a row from a QTableWidget using removeRow(...) and the application crashes. When I remove a row other than row 0, it usually doesn't crash, however removing row 0 always crashes.

The application crashes with:
ASSERT failure in QList<T>::operator[]: "index out of range", file c:/Qt/2009.03/qt/include/QtCore/../../src/corelib/tools/qlist.h, line 403

I saw a similar situation on this forum, with no real solution.

Any replies are appreciated. Thank you.

navi1084
10th July 2009, 07:58
before removing the row check the current Index. Seems to be deleting the item which are not there in the list. Use currentRow() and check whether return value is -1 or 0. if return value is -1, then if u r trying to delete definitely application crashes

nish
10th July 2009, 08:05
can you post a little bit of your code?

inktomi
10th July 2009, 08:10
currentRow() is returning the correct value.

Here's the code, triggered when a button is clicked. The row index is correct because I printed the value in the console. I even hardcoded the application to remove row index 0 and it still crashes.


void MainWindow::on_removeButton_clicked()
{
QList<QTableWidgetItem *> selectedItems = ui->clipListWidget->selectedItems();
if ((ui->clipListWidget->rowCount() > 0) && (selectedItems.count() > 0))
{
ui->clipListWidget->removeRow(selectedItems[0]->row());
}
}

nish
10th July 2009, 08:30
try to clear the selection of item first before removing


selectedItems[0]->setSelected(false);
ui->clipListWidget->removeRow(selectedItems[0]->row());

also make sure the you do not access the item anywhere else

if you single step in debugger .. on which line it crashes?

inktomi
10th July 2009, 08:55
It crashes on:


ui->clipListWidget->removeRow(selectedItems[0]->row());

Apparently its not letting me modify any properties of selected items. Not even unselecting selected items.

nish
10th July 2009, 08:57
ok.. can you produce a minimal compilable example reproducing the problem?

inktomi
10th July 2009, 09:49
I've tried to reprodce it but it is not happening.

I've now tried to unselect all items one after the other. All is fine until I unselect the last item. When I do, I get the 'index out of range' crash.

EDIT: Turns out that the crash was caused in the SelectionChanged event. When the last row was being removed, the selection event was being triggered, but it wasn't handling the 'nothing selected' case correctly.

inktomi
10th July 2009, 09:57
Sorry for bothering you. Seems I still can't use the debugger correctly.

Nickolay
17th June 2010, 11:43
I'm also have such problem - crash on deleting row = 0.
Also read many topics and try different decisions,
but nothing help me.

But seems solutions is found,at least for me it sufficient:

in QtDesigner -> property page of QTableWidget and set -> SelectionBehavior -> SelectRows

tableNotifications->setSelectionBehavior(QAbstractItemView::SelectRows );
due to this, now I call deleteRow(0) and all works! without crash.

wysota
17th June 2010, 12:59
Be sure not to delete items from within slots connected to signals that have the item (or its index) as their parameter.