Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove selected items from listWidget

How to remove selected items from a QListWidget?

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

QList<QListWidgetItem*> items = ui->listWidget->selectedItems(); foreach(QListWidgetItem item, items){     ui->listWidget->removeItemWidget(item); } 
like image 359
Lion King Avatar asked Aug 21 '14 02:08

Lion King


People also ask

How to remove item from QListWidget?

You may use this code to remove: QListWidgetItem *it = ui->listWidget->takeItem(ui->listWidget->currentRow()); delete it; Keep in mind that "takeItem" does not delete the object. Hi, friend, welcome devnet.

How to remove item from QListWidget pyqt5?

To remove items from the list, use takeItem() . The current item in the list can be found with currentItem() , and changed with setCurrentItem() . The user can also change the current item by navigating with the keyboard or clicking on a different item.


1 Answers

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()); 
like image 162
Nejat Avatar answered Oct 04 '22 05:10

Nejat