Remove selected items from listWidget

此生再无相见时 提交于 2019-12-09 14:06:50

问题


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 ?


回答1:


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



回答2:


To give a solution with removeItemWidget:

QList<QListWidgetItem*> items = ui->listWidget->selectedItems();

foreach(QListWidgetItem* item, items){
    ui->listWidget->removeItemWidget(item);
    delete item; // Qt documentation warnings you to destroy item to effectively remove it from QListWidget.
}


来源:https://stackoverflow.com/questions/25417348/remove-selected-items-from-listwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!