QListWidgetItem removal does not work for the last element
-
This is my code:
void MainWindow::on_pushButtonDELETE_clicked() { QList<QListWidgetItem*> items = ui->listWidget->selectedItems(); foreach(QListWidgetItem * item, items) { delete ui->listWidget->takeItem(ui->listWidget->row(item)); } }
There is no NULL pointer passed to the ui->listWidget->takeItem.
It removes item on the list but when there is one item on the list, removing causes"Invalid parameter passed to C runtime function" and program crashes.
Where is the problem of that? -
Hi
Tried the same code in Qt5.13 with a clean project and a ListWidget and nothing
bad happened.
Just deleted the single line. I tried adding many and delete some of them, then the last.
All cases worked. -
Hi,
What OS are you running ?
-
Hi
Did you try the same code in clean project to validate its actually that? -
This is my code:
void MainWindow::on_pushButtonDELETE_clicked() { QList<QListWidgetItem*> items = ui->listWidget->selectedItems(); foreach(QListWidgetItem * item, items) { delete ui->listWidget->takeItem(ui->listWidget->row(item)); } }
There is no NULL pointer passed to the ui->listWidget->takeItem.
It removes item on the list but when there is one item on the list, removing causes"Invalid parameter passed to C runtime function" and program crashes.
Where is the problem of that?@BartoszT
Although your code is actually supposed to be probably OK, especially since @mrjj says it worked fine for him, there occurs to me small possibility. You are deleting all items on list, just maybe theforeach
has an issue moving off the last one it has deleted because there are no remaining items. As I say I doubt it, but why not try deleting by, say, index instead? Along the lines of:int num = items.count(); for (int i = 0; i < num; i++) delete items.takeAt(0); //Always delete element 0
Any difference in behaviour?
Separately, make sure your own code does not have, say, a slot on list item removal which relies on there still being one selected item and crashes if not? If you can, running it inside debugger should show you where this error/crash is emanating from.
-
Thanks for the advice,
I also use the on_listWidget_currentRowChanged slot, after deleting the last item in the list, the current currentRow number assumes the value -1 and it causes a problem. The list is linked to the pointers container and the data is fetched from the object associated with the pointer. -
@BartoszT said in QListWidgetItem removal does not work for the last element:
currentRowChanged
QItemSelectionModel::currentRowChanged() will emit an invalid QModelIndex when there is nothing selected so -1 is correct here.
Please show us a minimal compilable example since noone except you can reproduce a problem here. -
@BartoszT
Yes, well (with the check line commented out) goingTachoList.at(-1)
is going to be the source of your ""Invalid parameter passed to C runtime function" and program crashes" problem!@Christian-Ehrlicher has said
currentRow
can indeed be -1, so it's your responsibility to deal with that in whatever code you add.Which is why I originally wondered whether you had code elsewhere relying on there being at least one selected item :)