Map: don't fully reset model on selection change

When changing the selection the MapLocationModel was reset.
This lead to crashes on Qt-5.9 which are due to QML accessing
data that was freed during model reset. This putative Qt bug
doesn't happen on newer Qt versions. At least Qt-5.12 is known
to work.

Instead of fighting the bug, let's simply not reset the model
but send a dataChanged() for every element of the MapLocationModel.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-08-30 16:51:59 +02:00 committed by Dirk Hohndel
parent cabe70be80
commit b39f2406c6
7 changed files with 25 additions and 1 deletions

View file

@ -464,7 +464,7 @@ void MainWindow::selectionChanged()
configureToolbar(); configureToolbar();
enableDisableOtherDCsActions(); enableDisableOtherDCsActions();
} }
MapWidget::instance()->reload(); MapWidget::instance()->selectionChanged();
} }
void MainWindow::on_actionNew_triggered() void MainWindow::on_actionNew_triggered()

View file

@ -84,6 +84,13 @@ bool MapWidget::editMode() const
return isReady && m_mapHelper->editMode(); return isReady && m_mapHelper->editMode();
} }
void MapWidget::selectionChanged()
{
CHECK_IS_READY_RETURN_VOID();
m_mapHelper->selectionChanged();
m_mapHelper->centerOnSelectedDiveSite();
}
void MapWidget::selectedDivesChanged(const QList<int> &list) void MapWidget::selectedDivesChanged(const QList<int> &list)
{ {
CHECK_IS_READY_RETURN_VOID(); CHECK_IS_READY_RETURN_VOID();

View file

@ -23,6 +23,7 @@ public:
static MapWidget *instance(); static MapWidget *instance();
void reload(); void reload();
void selectionChanged();
bool editMode() const; bool editMode() const;
public slots: public slots:

View file

@ -113,6 +113,11 @@ void MapWidgetHelper::reloadMapLocations()
m_mapLocationModel->reload(m_map); m_mapLocationModel->reload(m_map);
} }
void MapWidgetHelper::selectionChanged()
{
m_mapLocationModel->selectionChanged();
}
void MapWidgetHelper::selectedLocationChanged(struct dive_site *ds_in) void MapWidgetHelper::selectedLocationChanged(struct dive_site *ds_in)
{ {
int idx; int idx;

View file

@ -36,6 +36,7 @@ public:
Q_INVOKABLE void updateCurrentDiveSiteCoordinatesFromMap(struct dive_site *ds, QGeoCoordinate coord); Q_INVOKABLE void updateCurrentDiveSiteCoordinatesFromMap(struct dive_site *ds, QGeoCoordinate coord);
Q_INVOKABLE void selectVisibleLocations(); Q_INVOKABLE void selectVisibleLocations();
Q_INVOKABLE void selectedLocationChanged(struct dive_site *ds); Q_INVOKABLE void selectedLocationChanged(struct dive_site *ds);
void selectionChanged();
QString pluginObject(); QString pluginObject();
bool editMode() const; bool editMode() const;

View file

@ -150,6 +150,15 @@ static bool hasSelectedDive(const dive_site *ds)
[] (const dive *d) { return d->selected; }); [] (const dive *d) { return d->selected; });
} }
void MapLocationModel::selectionChanged()
{
if (m_mapLocations.isEmpty())
return;
for(MapLocation *m: m_mapLocations)
m->m_selected = m_selectedDs.contains(m->divesite());
emit dataChanged(createIndex(0, 0), createIndex(m_mapLocations.size() - 1, 0));
}
void MapLocationModel::reload(QObject *map) void MapLocationModel::reload(QObject *map)
{ {
beginResetModel(); beginResetModel();

View file

@ -67,6 +67,7 @@ public:
void add(MapLocation *); void add(MapLocation *);
// If map is not null, it will be used to place new dive sites without GPS location at the center of the map // If map is not null, it will be used to place new dive sites without GPS location at the center of the map
void reload(QObject *map); void reload(QObject *map);
void selectionChanged();
MapLocation *getMapLocation(const struct dive_site *ds); MapLocation *getMapLocation(const struct dive_site *ds);
const QVector<dive_site *> &selectedDs() const; const QVector<dive_site *> &selectedDs() const;
Q_INVOKABLE void setSelected(struct dive_site *ds); Q_INVOKABLE void setSelected(struct dive_site *ds);