C++ 从 listWidget 中删除所选项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25417348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove selected items from listWidget
提问by Lion King
How to remove selected items from qlistWidget
.
如何从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
?
现在,如何删除我从qlistWidget
?
回答by Nejat
One way to remove item from QListWidget
is to use QListWidget::takeItem
which removes and returns the item :
删除项目的一种方法QListWidget
是使用QListWidget::takeItem
which 删除并返回项目:
QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
foreach(QListWidgetItem * item, items)
{
delete ui->listWidget->takeItem(ui->listWidget->row(item));
}
Another way is to qDeleteAll
:
另一种方法是qDeleteAll
:
qDeleteAll(ui->listWidget->selectedItems());
回答by glihm
To give a solution with removeItemWidget
:
给出一个解决方案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.
}