user63898
user63898

Reputation: 30895

In QListWidget how do i check if QListWidgetItem already exists based on its Data member

im setting item's in QListWidget and in each QListWidgetItem im setting id like this:

newItem->setData(Qt::DisplayRole,ID);

now each time before im adding the itemi wish to check if already there is item with the same data in the list . how can i do that ... i don't think the findItems will help me here

Upvotes: 1

Views: 4558

Answers (1)

maverik
maverik

Reputation: 5586

Let me assume that type of ID is int (cause you didn't specify it).

bool found = false;
for (int i = 0; i < list->count(); ++i) {
    if (list->item(i)->data(Qt::DisplayRole).toInt() == ID_to_match) {
        found = true;
        break;
    }
}

if (!found) {
    do_something_here();
}

Upvotes: 3

Related Questions