Remove selected items from listWidget

Lion King picture Lion King · Aug 21, 2014 · Viewed 27.3k times · Source

How to remove selected items from qlistWidget.

I have tried write the following code, but does not work.

QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
foreach(QListWidgetItem item, items){
    ui->listWidget->removeItemWidget(item);
}

Now, how to remove the items that I selected from the qlistWidget ?

Answer

Nejat picture Nejat · Aug 21, 2014

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());