in this program, items (markers
) are added to a QListWidget calles ui->lwMarkers
. These items can also be removed again by pressing the "Remove button" which calls the following function
void Form::on_pbRemoveMarker_clicked()
{
if (ui->lwMarkers->currentRow() < 0) return;
delete ui->lwMarkers->takeItem(ui->lwMarkers->currentRow());
}
Inside the function, the first line is to make sure an item (marker) is actually selected. The second line is (at least, I hope) to delete the selected item.
Adding and removing: all goes well, unless when you want to remove next-to-last item. Then it crashes, unfortunately. I do not see why.
Can anyone shed a light on this issue?
If it can help: the full code is from the qt-google-maps project: https://code.google.com/p/qt-google-maps/ . This project uses the Google Maps API v2, I altered the code to use v3.
The question that I ask, is a particular behaviour of their code, and I simply don't see the reason of the crash. Any help?
The crash always happens just before the delete
, I believe it is because of the takeItem
and the error I get is as follows:
ASSERT failure in QList::operator[]: "index out of range", file ../../QtSDK/Desktop/Qt/473/gcc/include/QtCore/qlist.h, line 464
ui->lwMarkers->currentRow()
is not returning from 1 to N, where N is the number of rows? If that's the case you have to subtract one from the currentRow() to use on the take item. – Felipe TonellocurrentRow()
returns 0 to N-1 when a row is active/selected . When no row is active/selected, it returns -1 (therefore theif
-test) – Wim