@@ -135,21 +135,41 @@ QLimitProxyModelQML::sourceRowsAboutToBeInserted(const QModelIndex &parent, int
135135 const int nSourceAddedItems = end - start + 1;
136136 const int currentCount = QIdentityProxyModel::rowCount();
137137 if (currentCount + nSourceAddedItems <= m_limit) {
138+ // After Inserting items we will be under the limit
139+ // so just proceed with the insertion normally
138140 beginInsertRows(mapFromSource(parent), start, end);
139141 m_sourceInserting = true;
140− } else if (currentCount < m_limit) {
142+ } else if (currentCount >= m_limit) {
143+ // We are already over the limit so to our users we are not adding items, just
144+ // changing it's data, i.e we had something like
145+ // A B C D E
146+ // with a limit of 5
147+ // after inserting (let's say three 'F' at position 1) we will have
148+ // A F F F B
149+ // so we just need to signal a dataChanged from 1 to 4
150+ m_dataChangedBegin = start;
151+ m_dataChangedEnd = m_limit - 1;
152+ } else { // currentCount < m_limit && currentCount + nSourceAddedItems > m_limit
153+ // We have less items than the limit but after adding them we will be over
154+ // To our users this means we need to insert some items and change the
155+ // data of some others, i.e we had something like
156+ // A B C
157+ // with a limit of 5
158+ // after inserting (let's say three 'F' at position 1) we will have
159+ // A F F F B
160+ // so we need to signal an insetion from position 1 to 2, instead of from
161+ // position 1 to 3 and a after that a data changed from 3 to 4
141162 const int nItemsToInsert = m_limit - currentCount;
142163 beginInsertRows(mapFromSource(parent), start, start + nItemsToInsert - 1);
143164 m_sourceInserting = true;
144165 m_dataChangedBegin = start + nItemsToInsert;
145166 m_dataChangedEnd = m_limit - 1;
146167 if (m_dataChangedBegin > m_dataChangedEnd) {
168+ // Just in case we were empty and insert 6 items with a limit of 5
169+ // We don't want to signal a dataChanged from 5 to 4
147170 m_dataChangedBegin = -1;
148171 m_dataChangedEnd = -1;
149172 }
150− } else {
151− m_dataChangedBegin = start;
152− m_dataChangedEnd = m_limit - 1;
153173 }
154174 }
155175}
@@ -164,9 +184,32 @@ QLimitProxyModelQML::sourceRowsAboutToBeRemoved(const QModelIndex &parent, int s
164184 const int nSourceRemovedItems = end - start + 1;
165185 const int currentCount = QIdentityProxyModel::rowCount();
166186 if (currentCount <= m_limit) {
187+ // We are already under the limit so
188+ // so just proceed with the removal normally
167189 beginRemoveRows(mapFromSource(parent), start, end);
168190 m_sourceRemoving = true;
169− } else if (currentCount - nSourceRemovedItems < m_limit) {
191+ } else if (currentCount - nSourceRemovedItems >= m_limit) {
192+ // Even after removing items we will be at or over the limit
193+ // So to our users we are not removing anything, just changing the data
194+ // i.e. we had a internal model with
195+ // A B C D E F G H
196+ // and a limit of 5, our users just see
197+ // A B C D E
198+ // so if we remove 3 items starting at 1 we have to expose
199+ // A E F G H
200+ // that is, a dataChanged from 1 to 4
201+ m_dataChangedBegin = start;
202+ m_dataChangedEnd = m_limit - 1;
203+ } else { // currentCount > m_limit && currentCount - nSourceRemovedItems < m_limit
204+ // We have more items than the limit but after removing we will be below it
205+ // So to our users we both removing and changing the data
206+ // i.e. we had a internal model with
207+ // A B C D E F G
208+ // and a limit of 5, our users just see
209+ // A B C D E
210+ // so if we remove items from 1 to 3 we have to expose
211+ // A E F G
212+ // that is, a remove from 4 to 4 and a dataChanged from 1 to 3
170213 const int nItemsToRemove = m_limit - (currentCount - nSourceRemovedItems);
171214 beginRemoveRows(mapFromSource(parent), m_limit - nItemsToRemove, m_limit - 1);
172215 m_sourceRemoving = true;
@@ -176,9 +219,6 @@ QLimitProxyModelQML::sourceRowsAboutToBeRemoved(const QModelIndex &parent, int s
176219 m_dataChangedBegin = -1;
177220 m_dataChangedEnd = -1;
178221 }
179− } else {
180− m_dataChangedBegin = start;
181− m_dataChangedEnd = m_limit - 1;
182222 }
183223 }
184224}