From 2c11544d9321398f67469085257db6b6114fb992 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 7 Nov 2019 00:29:36 +0100 Subject: [PATCH] Mobile: correctly update filter text and update all three models This is even harder because setActiveTrip is called from an action slot from QML. If the C++ code called from that slot causes the object to which this slot belongs to be destroyed, we get very strange crashes. The only workaround I could come up with was to update the filter asynchronously. This all seems very ugly and fragile. Signed-off-by: Dirk Hohndel --- qt-models/divelistmodel.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/qt-models/divelistmodel.cpp b/qt-models/divelistmodel.cpp index 37e48b023..7d97359f0 100644 --- a/qt-models/divelistmodel.cpp +++ b/qt-models/divelistmodel.cpp @@ -7,6 +7,7 @@ #include "core/ssrf.h" // for LOG_STP #include "core/errorhelper.h" // for verbose #include +#include #include // the DiveListSortModel creates the sorted, filtered list of dives that the user @@ -101,8 +102,14 @@ QString CollapsedDiveListSortModel::tripShortDate(const QString §ion) void CollapsedDiveListSortModel::setActiveTrip(const QString &trip) { m_activeTrip = trip; - updateFilterState(); - invalidateFilter(); + // we can't update the filter state from the this function as that is called from + // a slot in the QML code which could cause the object that is executing the slot + // to be destroyed before this function returns. + // Instead do this asynchronously + QtConcurrent::run(QThreadPool::globalInstance(), + [=]{ + CollapsedDiveListSortModel::instance()->updateFilterState(); + }); } QString CollapsedDiveListSortModel::activeTrip() const @@ -153,8 +160,8 @@ void CollapsedDiveListSortModel::updateFilterState() } // everything up to here can be done even if we don't have a source model if (sourceModel() != nullptr) { - QVector changedRoles = { DiveListModel::CollapsedRole }; - dataChanged(index(0,0), index(rowCount() - 1, 0), changedRoles); + DiveListModel *dlm = DiveListModel::instance(); + dlm->dataChanged(dlm->index(0,0), dlm->index(dlm->rowCount() - 1, 0)); } } @@ -220,7 +227,7 @@ void DiveListSortModel::setSourceModel(QAbstractItemModel *sourceModel) void DiveListSortModel::setFilter(QString f) { filterString = f; - updateFilterState(); + CollapsedDiveListSortModel::instance()->updateFilterState(); invalidateFilter(); }