从 listWidget 中删除所选项目 [英] Remove selected items from listWidget

查看:64
本文介绍了从 listWidget 中删除所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 qlistWidget 中删除选定的项目.

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

现在,如何删除我从 qlistWidget 中选择的项目?

Now, how to remove the items that I selected from the qlistWidget ?

推荐答案

QListWidget 中删除项目的一种方法是使用 QListWidget::takeItem 删除并返回项目:

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

另一种方法是 qDeleteAll :

qDeleteAll(ui->listWidget->selectedItems());

这篇关于从 listWidget 中删除所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆