Search code examples
c++qtqlistwidgetqlistwidgetitem

Delete QListWidget item by text content


I need to delete a qlistwidget item by text content

I tried:

QString mstring =  "Programmer II";

QList<QListWidgetItem *> items = ui->listJobs->findItems(mstring,  Qt::MatchExactly);
    if (items.size() > 0)
        ui->listJobs->takeItem( ui->listJobs->currentRow() );

...and various permutations, but I'm missing something. The code above compiles, but does not delete the item from the qlistwidget.


Solution

  • The code doesn't indicate what the value of currentRow is, but findItems doesn't set it, so it's unlikely to correlate with the value you're trying to remove. I don't see any way to use the results of findItems and get the row(s) you want to remove. I think you have to loop through the contents, compare the text of each item, and then remove the ones that match. You'll probably want to do the loop in reverse order; otherwise, once you've removed an item, the loop counter will no longer match the list item's row numbers.