From e83c707ad23d019d8d5fbf09b990bf0af490aabc Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Wed, 11 Jan 2017 17:19:17 +0100 Subject: [PATCH] Recreate the menu when devices change Instead of trying to be overly smart. Qt wasn't updating the StatusNotifier properly anyway, this makes the whole thing work. --- indicator/main.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/indicator/main.cpp b/indicator/main.cpp index 2453fe9e3..99ef35de4 100644 --- a/indicator/main.cpp +++ b/indicator/main.cpp @@ -49,26 +49,28 @@ int main(int argc, char** argv) DevicesModel model; model.setDisplayFilter(DevicesModel::Reachable | DevicesModel::Paired); - QMenu menu; - auto configure = menu.addAction(i18n("Configure...")); - QObject::connect(configure, &QAction::triggered, configure, [](){ - QProcess::startDetached("kcmshell5", {"kdeconnect"}); - }); - QSystemTrayIcon systray; systray.setIcon(QIcon::fromTheme("kdeconnect")); - systray.setContextMenu(&menu); systray.setVisible(true); - QObject::connect(&model, &DevicesModel::rowsInserted, &model, [&menu, &model](const QModelIndex& /*parent*/, int first, int last) { - for (int i=first; i<=last; ++i) { + auto refreshMenu = [&systray, &model]() { + QMenu *menu = new QMenu; + auto configure = menu->addAction(i18n("Configure...")); + QObject::connect(configure, &QAction::triggered, configure, [](){ + QProcess::startDetached("kcmshell5", {"kdeconnect"}); + }); + for (int i=0, count = model.rowCount(); iaddMenu(indicator); } - }); + systray.setContextMenu(menu); + }; + + QObject::connect(&model, &DevicesModel::rowsInserted, &model, refreshMenu); + QObject::connect(&model, &DevicesModel::rowsRemoved, &model, refreshMenu); QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() { systray.setToolTip(i18np("%1 device connected", "%1 devices connected", model.rowCount()));