From 952ab431d7fb9f660438da612d2b00485cce02f3 Mon Sep 17 00:00:00 2001 From: Paolo Angelelli Date: Thu, 29 Nov 2018 15:25:28 +0100 Subject: Fix MapObjectView removing wrong indices on model changes No incubation cancellation required when an object is removed: the DM takes care of that. Handle also the case of itemCreated called during synchronous object creation, that fires the callback in the middle of the ->object call. Task-number: QTBUG-71264 Change-Id: I058a101c754f22f4b6fbcbd7f6f7ded36f3c129b Reviewed-by: Alex Blasche --- src/location/labs/qmapobjectview.cpp | 28 +++++++++++++++++++--------- src/location/labs/qmapobjectview_p.h | 3 ++- 2 files changed, 21 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/location/labs/qmapobjectview.cpp b/src/location/labs/qmapobjectview.cpp index 2ffc27bc..fc583415 100644 --- a/src/location/labs/qmapobjectview.cpp +++ b/src/location/labs/qmapobjectview.cpp @@ -153,8 +153,8 @@ void QMapObjectView::classBegin() QQmlInstanceModel *model = m_delegateModel; connect(model, &QQmlInstanceModel::modelUpdated, this, &QMapObjectView::modelUpdated); connect(model, &QQmlInstanceModel::createdItem, this, &QMapObjectView::createdItem); - connect(model, &QQmlInstanceModel::destroyingItem, this, &QMapObjectView::destroyingItem); - connect(model, &QQmlInstanceModel::initItem, this, &QMapObjectView::initItem); +// connect(model, &QQmlInstanceModel::destroyingItem, this, &QMapObjectView::destroyingItem); +// connect(model, &QQmlInstanceModel::initItem, this, &QMapObjectView::initItem); } void QMapObjectView::componentComplete() @@ -278,11 +278,12 @@ void QMapObjectView::modelUpdated(const QQmlChangeSet &changeSet, bool reset) } } + QBoolBlocker createBlocker(m_creatingObject, true); for (const QQmlChangeSet::Change &c: changeSet.inserts()) { for (int idx = c.start(); idx < c.end(); idx++) { m_instantiatedMapObjects.insert(idx, nullptr); QGeoMapObject *mo = qobject_cast(m_delegateModel->object(idx, incubationMode)); - if (mo) // if not, a createdItem signal will be emitted. + if (mo) // if not, a createdItem signal will be emitted later, else it has been emitted already while createBlocker is in effect. addMapObjectToMap(mo, idx); } } @@ -311,12 +312,17 @@ void QMapObjectView::removeMapObjectFromMap(int index) { if (index >= 0 && index < m_instantiatedMapObjects.size()) { QGeoMapObject *mo = m_instantiatedMapObjects.takeAt(index); - if (!mo) { - m_delegateModel->cancel(index); + if (!mo) return; - } + mo->setMap(nullptr); - m_delegateModel->release(mo); + QQmlInstanceModel::ReleaseFlags releaseStatus = m_delegateModel->release(mo); +#ifdef QT_DEBUG + if (releaseStatus == QQmlInstanceModel::Referenced) + qWarning() << "object "<(m_delegateModel->object(index, incubationMode)); if (mo) addMapObjectToMap(mo, index); + else + qWarning() << "QQmlDelegateModel::object called in createdItem for " << index << " produced a null object"; } diff --git a/src/location/labs/qmapobjectview_p.h b/src/location/labs/qmapobjectview_p.h index 49b80883..76affced 100644 --- a/src/location/labs/qmapobjectview_p.h +++ b/src/location/labs/qmapobjectview_p.h @@ -107,8 +107,9 @@ protected: QQmlComponent *m_delegate = nullptr; QQmlDelegateModel *m_delegateModel = nullptr; QVector> m_instantiatedMapObjects; - QVector> m_pendingMapObjects; + QVector> m_pendingMapObjects; // for items instantiated before the map is set QVector> m_userAddedMapObjects; // A third list containing the objects dynamically added through addMapObject + bool m_creatingObject = false; }; QT_END_NAMESPACE -- cgit v1.2.1