Bug 365105 - QSystemTrayIcon with context menu does not react after second show
Summary: QSystemTrayIcon with context menu does not react after second show
Status: RESOLVED FIXED
Alias: None
Product: plasmashell
Classification: Plasma
Component: System Tray (show other bugs)
Version: 5.6.5
Platform: Arch Linux Linux
: NOR normal
Target Milestone: 1.0
Assignee: Plasma Bugs List
URL:
Keywords:
: 411851 (view as bug list)
Depends on:
Blocks:
 
Reported: 2016-07-05 13:53 UTC by Alex Bikadorov
Modified: 2019-11-05 11:43 UTC (History)
7 users (show)

See Also:
Latest Commit:
Version Fixed In: 5.18
Sentry Crash Report:


Attachments
Demo code (803 bytes, text/x-c++src)
2016-07-05 14:51 UTC, Alex Bikadorov
Details
Cmake file (466 bytes, text/plain)
2016-07-05 14:51 UTC, Alex Bikadorov
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Bikadorov 2016-07-05 13:53:51 UTC
During development with Qt I noticed a bug in the Plasma's system tray when using QSystemTrayIcon:
If the system tray has a context menu set and is shown after one hide (show->hide->show) the tray doesn't react anymore:
* no context menu shown on right click
* left click doesn't emit signal

Reproducible: Always

Steps to Reproduce:
1.Try this code inside a QMainWindow:

        QSystemTrayIcon *sysTray = new QSystemTrayIcon(this);
        sysTray->setIcon(style()->standardIcon(QStyle::SP_DesktopIcon));
        QMenu *trayMenu = new QMenu(this);

        sysTray->setContextMenu(trayMenu);

        connect(sysTray, &QSystemTrayIcon::activated,
                [=](QSystemTrayIcon::ActivationReason){ qDebug() << "clicked"; });

        sysTray->show();
        sysTray->hide();
        sysTray->show();

2. compile
3. run:)

Actual Results:  
left click on tray does not do anything ("clicked" not printed)

Expected Results:  
print "clicked" to std output

This does not happen in LXQt, so I assume it is related to KF5.
Comment 1 David Edmundson 2016-07-05 14:35:00 UTC
Could you just upload a small demo app as you've clearly written one.
Comment 2 Alex Bikadorov 2016-07-05 14:51:24 UTC
Created attachment 99860 [details]
Demo code
Comment 3 Alex Bikadorov 2016-07-05 14:51:45 UTC
Created attachment 99861 [details]
Cmake file
Comment 4 Alex Bikadorov 2016-07-05 14:53:11 UTC
You can also use the Qt example: https://doc.qt.io/qt-5/qtwidgets-desktop-systray-example.html

Same bug there
Comment 5 David Edmundson 2016-07-14 21:58:01 UTC
confirmed. 

unset XDG_CURRENT_DESKTOP 
unset KDE_FULL_SESSION 

so that it doesn't use our QPT fixes it - so the bug is probably in our plasma-integration lib
Comment 6 David Edmundson 2016-07-15 21:44:52 UTC
it's getting inside the first if() in here:


void KStatusNotifierItemDBus::Activate(int x, int y)
{
    if (m_statusNotifierItem->d->associatedWidget == m_statusNotifierItem->d->menu) {
        ContextMenu(x, y);
    } else {
        m_statusNotifierItem->activate(QPoint(x, y));
    }
}


The reason being d->assoociatedWidget is null
and d->menu is null

hence it matches, but it's not really what that code is trying to do.

Doing the obvious fix on that fixes the bug. (plus again in KStatusNotifierItem::activate where the code is repeated for no reason)

However, d->menu shouldn't  be null - you're clearly setting one.

Will investigate that - or maybe just get rid of the plasma SNI integration given that's in Qt now..
Comment 7 David Edmundson 2016-07-16 18:13:10 UTC
Update:

We have one menu for the whole tray instance
We create and delete KSNI object in init() and cleanup() which are called on show() hide()

The SNI takes ownership of the menu

so for the the second init, we set the SNI to a now deleted menu.

It doesn't crash because it's wrapped in a QWeakpointer + guards as this came up before and someone did a bodge job of fixing it in ee62e7d9d2fe185f00562a74602aa4c19e8a88c7 fixing the symptoms without identifying the root cause.
Comment 8 Alex Bikadorov 2016-10-12 17:06:26 UTC
Ehm, any news here?
Comment 9 Alex Bikadorov 2016-10-28 17:15:07 UTC
Git commit 4b3d3b3ab21cff913a0529fddabe94744f607b95 by Alexander Bikadorov.
Committed on 28/10/2016 at 17:14.
Pushed by abikadorov into branch 'master'.

FIXED: [ 365105 ] Workaround for bug in KF5+QSystemTrayIcon

M  +15   -0    krusader/krusader.cpp

http://commits.kde.org/krusader/4b3d3b3ab21cff913a0529fddabe94744f607b95
Comment 10 Alex Bikadorov 2016-10-28 17:16:57 UTC
sorry, closed the wrong bug.
Comment 11 Pavlo84 2017-12-22 19:59:40 UTC
Confirmed that have the same situation with tray on Archlinux Plasma 5.11.4

Whats the status of bug?
Has anybody news?
Last comment was 2 month ago.
Comment 12 Konrad Materka 2019-10-14 18:29:00 UTC
*** Bug 411851 has been marked as a duplicate of this bug. ***
Comment 13 Konrad Materka 2019-10-17 15:34:48 UTC
Git commit aa6bb9b06cfba67c1ba226d053a49e0bc1b69cad by Konrad Materka.
Committed on 17/10/2019 at 15:34.
Pushed by kmaterka into branch 'master'.

[KStatusNotifierItem] Allow left click when menu is null

Summary:
If associatedWidget and menu are the same then instead of "activate"
action context menu is used. When both are null coparition is always
true but context menu can't be shown, since it is null.
It is partial solution for a problem described in https://bugs.kde.org/show_bug.cgi?id=365105.

Test Plan:
As described in the bug:
* create QSystemTrayIcon
* assign QMenu
* show, hide and show again
Both right click and left click do nothing.

After change left click works

Reviewers: davidedmundson, broulik, nicolasfella, #frameworks

Reviewed By: davidedmundson

Subscribers: kde-frameworks-devel

Tags: #frameworks

Differential Revision: https://phabricator.kde.org/D24667

M  +2    -2    src/kstatusnotifieritem.cpp
M  +1    -5    src/kstatusnotifieritemdbus_p.cpp

https://commits.kde.org/knotifications/aa6bb9b06cfba67c1ba226d053a49e0bc1b69cad
Comment 14 Konrad Materka 2019-11-05 11:42:53 UTC
Git commit bc1c85144adb9a40ed8a6a38ac84234c738dbc7f by Konrad Materka.
Committed on 05/11/2019 at 11:42.
Pushed by kmaterka into branch 'master'.

[KDEPlatformSystemTrayIcon] Recreate deleted menu

Summary:
Recreate internal menu when needed.
It is possible that internal representation of system tray menu is
deleted. This can happen when QSystemTrayIcon is hidden.

Test Plan: Unit tests included

Reviewers: apol, davidedmundson, #plasma, #frameworks, broulik, nicolasfella

Reviewed By: apol

Subscribers: plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D24843

M  +10   -2    autotests/CMakeLists.txt
A  +106  -0    autotests/kdeplatformsystemtrayicon_unittest.cpp     [License: LGPL (v2/3+eV)]
M  +40   -21   src/platformtheme/kdeplatformsystemtrayicon.cpp
M  +8    -1    src/platformtheme/kdeplatformsystemtrayicon.h

https://commits.kde.org/plasma-integration/bc1c85144adb9a40ed8a6a38ac84234c738dbc7f