How to remove selected items from qlistWidget
.
I have tried write the following code, but does not work.
QList items
One way to remove item from QListWidget
is to use QListWidget::takeItem
which removes and returns the item :
QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
foreach(QListWidgetItem * item, items)
{
delete ui->listWidget->takeItem(ui->listWidget->row(item));
}
Another way is to qDeleteAll
:
qDeleteAll(ui->listWidget->selectedItems());
To give a solution with removeItemWidget
:
QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
foreach(QListWidgetItem* item, items){
ui->listWidget->removeItemWidget(item);
delete item; // Qt documentation warnings you to destroy item to effectively remove it from QListWidget.
}