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;
});
}
Here is the solution I found:
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;
});
if(iter != m_feedItems.end())
{
int indx = m_feedItems.indexOf(*iter);
m_feedItems[indx] = feedItem;
dataChanged(index(indx), index(indx));
}
}