Remove selected items from listWidget

后端 未结 2 1039
遇见更好的自我
遇见更好的自我 2021-02-18 13:35

How to remove selected items from qlistWidget.

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

QList items         


        
相关标签:
2条回答
  • 2021-02-18 13:48

    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());
    
    0 讨论(0)
  • 2021-02-18 14:10

    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.
    }
    
    0 讨论(0)
提交回复
热议问题