QPlatformSystemTrayIcon: Add signal contextMenuRequested()

Add a signal passing the global position of the context menu.
For platforms that do not provide native menus, QSystemTrayIcon
will show a QMenu based menu.

Change-Id: I799e4a84ca38e00ea33f3c842ea4ca43ecb8c83f
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
Reviewed-by: Maurice Kalinowski <maurice.kalinowski@qt.io>
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Friedemann Kleint 2017-04-13 12:25:47 +02:00
parent 940b36850d
commit 121a30ccef
4 changed files with 31 additions and 1 deletions

View File

@ -139,11 +139,20 @@ QPlatformSystemTrayIcon::~QPlatformSystemTrayIcon()
Returns \c true if the system tray supports messages on the platform.
*/
/*!
\fn void QPlatformSystemTrayIcon::contextMenuRequested(QPoint globalPos, const QPlatformScreen *screen)
This signal is emitted when the context menu is requested.
In particular, on platforms where createMenu() returns nullptr,
its emission will cause QSystemTrayIcon to show a QMenu-based menu.
\sa activated()
\since 5.10
*/
/*!
\fn void QPlatformSystemTrayIcon::activated(QPlatformSystemTrayIcon::ActivationReason reason)
This signal is emitted when the user activates the system tray icon.
\a reason specifies the reason for activation.
\sa QSystemTrayIcon::ActivationReason
\sa QSystemTrayIcon::ActivationReason, contextMenuRequested()
*/
/*!

View File

@ -49,6 +49,7 @@
QT_BEGIN_NAMESPACE
class QPlatformMenu;
class QPlatformScreen;
class QIcon;
class QString;
class QRect;
@ -88,6 +89,7 @@ public:
Q_SIGNALS:
void activated(QPlatformSystemTrayIcon::ActivationReason reason);
void contextMenuRequested(QPoint globalPos, const QPlatformScreen *screen);
void messageClicked();
};

View File

@ -61,6 +61,9 @@
#include "qdesktopwidget.h"
#include "qbitmap.h"
#include <private/qhighdpiscaling_p.h>
#include <qpa/qplatformscreen.h>
QT_BEGIN_NAMESPACE
static QIcon messageIcon2qIcon(QSystemTrayIcon::MessageIcon icon)
@ -196,8 +199,23 @@ QSystemTrayIcon::~QSystemTrayIcon()
void QSystemTrayIcon::setContextMenu(QMenu *menu)
{
Q_D(QSystemTrayIcon);
QMenu *oldMenu = d->menu.data();
d->menu = menu;
d->updateMenu_sys();
if (oldMenu != menu && d->qpa_sys) {
// Show the QMenu-based menu for QPA plugins that do not provide native menus
if (oldMenu && !oldMenu->platformMenu())
QObject::disconnect(d->qpa_sys, &QPlatformSystemTrayIcon::contextMenuRequested, menu, nullptr);
if (menu && !menu->platformMenu()) {
QObject::connect(d->qpa_sys, &QPlatformSystemTrayIcon::contextMenuRequested,
menu,
[menu](QPoint globalNativePos, const QPlatformScreen *platformScreen)
{
QScreen *screen = platformScreen ? platformScreen->screen() : nullptr;
menu->popup(QHighDpi::fromNativePixels(globalNativePos, screen), nullptr);
});
}
}
}
/*!

View File

@ -334,6 +334,7 @@ bool QSystemTrayIconSys::winEvent( MSG *m, long *result )
QSystemTrayIconPrivate::QSystemTrayIconPrivate()
: sys(0),
qpa_sys(nullptr),
visible(false)
{
}