QListWidget::selectedItems returns a list of QListWidgetItem, but the only function for removing an item that I found is takeItem, which accepts only indexes, and selectedIndexes function is protected.
3 Answers
Try
qDeleteAll(listWidget->selectedItems());
-
This seems to work, but from the help it seems qDeleteAll is just a generic function that calls delete on a range of objects. Won't the QListWidget still think it has those items?– sashoalmAug 10, 2011 at 12:11
-
4
Iterate through the SelectedItemsList:
QList<QListWidgetItem *> itemList = widget->selectedItems();
for (int i=0; i<itemList.size(); i++) {
widget->takeItem(widget->indexFromItem(itemList[i]));
}
I think
widget->removeItemWidget(itemList[i]);
may also work
-
6From the Qt
takeItem
documentation: "Items removed from a list widget will not be managed by Qt, and will need to be deleted manually." So you should saydelete widget->takeItem(...);
– TonyKAug 10, 2011 at 10:35
ui->listWidget->clear(); will do asof qt5