diff --git a/src/models/appsmodel.cpp b/src/models/appsmodel.cpp index 2f45583a..a1b1f391 100644 --- a/src/models/appsmodel.cpp +++ b/src/models/appsmodel.cpp @@ -31,6 +31,7 @@ AppsModel::AppsModel(QObject *parent) connect(m_appInfoMonitor, &AppInfoMonitor::changed, this, [this](){ qDebug() << "changed"; QList items(allAppInfosShouldBeShown()); + cleanUpInvalidApps(items); QList duplicatedItems = updateItems(items); for (AppItem * item : qAsConst(duplicatedItems)) { delete item; @@ -136,3 +137,18 @@ QList AppsModel::allAppInfosShouldBeShown() const } return items; } + +// remove apps that are not in the \l knownExistedApps list +void AppsModel::cleanUpInvalidApps(const QList knownExistedApps) +{ + QSet existedApps; + for (const AppItem * app : knownExistedApps) { + existedApps.insert(app->freedesktopId()); + } + for (int i = rowCount() - 1; i >= 0; i--) { + const QString & appId(data(index(i, 0), AppItem::DesktopIdRole).toString()); + if (!existedApps.contains(appId)) { + removeRow(i); + } + } +} diff --git a/src/models/appsmodel.h b/src/models/appsmodel.h index d1b3fb15..2ea7a22b 100644 --- a/src/models/appsmodel.h +++ b/src/models/appsmodel.h @@ -40,6 +40,7 @@ class AppsModel : public QStandardItemModel explicit AppsModel(QObject *parent = nullptr); QList allAppInfosShouldBeShown() const; + void cleanUpInvalidApps(const QList knownExistedApps); AppInfoMonitor * m_appInfoMonitor;