--- a/src/model.cpp Tue Jun 07 21:35:29 2022 +0300 +++ b/src/model.cpp Wed Jun 08 19:33:00 2022 +0300 @@ -111,11 +111,24 @@ return pointerToOptional(findInMap(this->positions, id)); } +template<typename K, typename V> +void removeFromMap(std::map<K, V>& map, const K& key) +{ + const auto it = map.find(key); + if (it != map.end()) { + map.erase(it); + } +} + void Model::remove(int index) { if (index >= 0 and index < this->size()) { Q_EMIT this->beginRemoveRows({}, index, index); + removeFromMap(this->positions, this->body[index].id); this->body.erase(this->body.begin() + index); + for (int i = index; i < this->size(); ++i) { + this->positions[this->body[i].id] = i; + } Q_EMIT this->endRemoveRows(); } }