PDA

View Full Version : Out of bounds Qlist



seink
22nd November 2010, 13:24
Hello! a new noob is here ^^

I`m getting the following error as I use clear() in a QListWidget from my ui:


ASSERT failure in QList<T>::operator[]: "index out of range", file ../../QT/2010.05/qt/include/QtCore/../../src/corelib/tools/qlist.h, line 463


the QListWidget is adding itens correctly, but it crashes as I try to remove them.
I tried using takeItem inside a for but the same error ocurred
Please help.




void MyDialog::on_remove_my_bt_clicked()
{
int currentIdx = ui->my_list->currentIndex().row();
if(currentIdx>-1){
listA.removeAt(currentIdx);
if(listA.size()>0){
updateMyList();
}else{
ui->my_list->clear();//<- error here
listReady = false;
}
}
}

void MyDialog::updateMyList()
{
ui->my_list->clear();//<- error here
...
}

high_flyer
22nd November 2010, 13:36
Two things:
1. where is your assurance that the QListeWidget current index is also the index in the QList?
2. I can't remember if QList::removeAt() changes the lists length, if it does, I don't see where your design copes with it.

Just to test your code, and make it more defensive, put Q_ASSERT(currentIdx < listA.size()) before you call listA.removeAt(), I wont be surprised if the assert will fail.

seink
22nd November 2010, 15:31
found the problem ^^
the clear() throwed an currentItemChanged event, and I forgot to check the index of the current item on the listener(I only checked the previus one =p) XD

thanks for the answer, cya