Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove all the selected items in a QListWidget?

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.

like image 408
sashoalm Avatar asked Aug 10 '11 09:08

sashoalm


People also ask

How to delete items in QListWidget?

To achieve the required functionality i.e. to cleaning the window or deleting all elements using Qlistwidget in Python, its clear() method is used.

How do I clear the QT list widget?

QListWidget has a member named clear(). The docs for this method state: void QListWidget::clear () [slot] Removes all items and selections in the view. Warning: All items will be permanently deleted.


2 Answers

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

like image 35
Tobias Schlegel Avatar answered Oct 28 '22 09:10

Tobias Schlegel


Try

qDeleteAll(listWidget->selectedItems());
like image 142
O.C. Avatar answered Oct 28 '22 10:10

O.C.