How can I update a particular QAbstractListModel item?

Viewed 1112

I have derived a class FeedItemViewModel from QAbstractListModel. I have implemented a method which adds items in the list model but I do not know how too update an item which has a specific id.

Here is the code:

void FeedItemViewModel::addFeedItem(FeedItem* feedItem)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_feedItems.append(feedItem);
    endInsertRows();
}

void FeedItemViewModel::updateFeedItem(FeedItem* feedItem)
{
    int id = feedItem->id();
    auto iter = std::find_if(m_feedItems.begin(), m_feedItems.end(),
                         [id](FeedItem* item)
                         {
                            return item->id() == id;
                         });
}
2 Answers

If you are just updating an item and not replacing it, you could simplify by finding the pointer itself instead of using the id property


void FeedItemViewModel::updateFeedItem(FeedItem *feedItem){
    int indx = m_feedItems.indexOf(feedItem);
    if(indx != -1){
        dataChanged(index(indx), index(indx));
    }
}
Related