How can I empty a QListWidget without having it delete all of the QListItemWidgets it contains?

finiteloop picture finiteloop · Mar 7, 2012 · Viewed 13.4k times · Source

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.

How can I avoid all of the items being permanently deleted? I just want to clear the lists contents so that I can re-populate it with different data (however, I want to keep the data that was in it to display again if the user chooses to do so).

Answer

tmpearce picture tmpearce · Mar 7, 2012

QListWidget::takeItem

while(listwidget->count()>0)
{
  listwidget->takeItem(0);//handle the item if you don't 
                          //have a pointer to it elsewhere
}