Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquicklabsplatformsystemtrayicon.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
8
9#include <QtCore/qloggingcategory.h>
10#include <QtGui/qpa/qplatformtheme.h>
11#include <QtGui/private/qguiapplication_p.h>
12
13#include "widgets/qwidgetplatform_p.h"
14
16
17/*!
18 \qmltype SystemTrayIcon
19 \inherits QtObject
20//! \nativetype QQuickLabsPlatformSystemTrayIcon
21 \inqmlmodule Qt.labs.platform
22 \since 5.8
23 \brief A system tray icon.
24
25 The SystemTrayIcon type provides an icon for an application in the system tray.
26
27 Many desktop platforms provide a special system tray or notification area,
28 where applications can display icons and notification messages.
29
30 \image {qtlabsplatform-systemtrayicon.png} {System tray icon}
31
32 The following example shows how to create a system tray icon, and how to make
33 use of the \l activated() signal:
34
35 \code
36 SystemTrayIcon {
37 visible: true
38 icon.source: "qrc:/images/tray-icon.png"
39
40 onActivated: {
41 window.show()
42 window.raise()
43 window.requestActivate()
44 }
45 }
46 \endcode
47
48 \section2 Tray menu
49
50 SystemTrayIcon can have a menu that opens when the icon is activated.
51
52 \image {qtlabsplatform-systemtrayicon-menu.png} {System tray icon with menu}
53
54 The following example illustrates how to assign a \l Menu to a system tray icon:
55
56 \code
57 SystemTrayIcon {
58 visible: true
59 icon.source: "qrc:/images/tray-icon.png"
60
61 menu: Menu {
62 MenuItem {
63 text: qsTr("Quit")
64 onTriggered: Qt.quit()
65 }
66 }
67 }
68 \endcode
69
70 \section2 Notification messages
71
72 SystemTrayIcon can display notification messages.
73
74 \image {qtlabsplatform-systemtrayicon-message.png} {System tray icon message popup}
75
76 The following example presents how to show a notification message using
77 \l showMessage(), and how to make use of the \l messageClicked() signal:
78
79 \code
80 SystemTrayIcon {
81 visible: true
82 icon.source: "qrc:/images/tray-icon.png"
83
84 onMessageClicked: console.log("Message clicked")
85 Component.onCompleted: showMessage("Message title", "Something important came up. Click this to know more.")
86 }
87 \endcode
88
89 \section2 Availability
90
91 A native system tray icon is currently \l available on the following platforms:
92
93 \list
94 \li All window managers and independent tray implementations for X11 that implement the
95 \l{http://standards.freedesktop.org/systemtray-spec/systemtray-spec-0.2.html}
96 {freedesktop.org XEmbed system tray specification}.
97 \li All desktop environments that implement the
98 \l{http://www.freedesktop.org/wiki/Specifications/StatusNotifierItem/StatusNotifierItem}
99 {freedesktop.org D-Bus StatusNotifierItem specification}, including recent versions of KDE and Unity.
100 \li All supported versions of macOS. Note that the Growl notification system must be installed
101 for showMessage() to display messages on OS X prior to 10.8 (Mountain Lion).
102 \endlist
103
104 \input includes/widgets.qdocinc 1
105
106 \labs
107
108 \sa Menu
109*/
110
111/*!
112 \qmlsignal Qt.labs.platform::SystemTrayIcon::activated(ActivationReason reason)
113
114 This signal is emitted when the system tray icon is activated by the user. The
115 \a reason argument specifies how the system tray icon was activated.
116
117 Available reasons:
118
119 \value SystemTrayIcon.Unknown Unknown reason
120 \value SystemTrayIcon.Context The context menu for the system tray icon was requested
121 \value SystemTrayIcon.DoubleClick The system tray icon was double clicked
122 \value SystemTrayIcon.Trigger The system tray icon was clicked
123 \value SystemTrayIcon.MiddleClick The system tray icon was clicked with the middle mouse button
124*/
125
126/*!
127 \qmlsignal Qt.labs.platform::SystemTrayIcon::messageClicked()
128
129 This signal is emitted when a notification message is clicked by the user.
130
131 \sa showMessage()
132*/
133
134Q_STATIC_LOGGING_CATEGORY(qtLabsPlatformTray, "qt.labs.platform.tray")
135
136QQuickLabsPlatformSystemTrayIcon::QQuickLabsPlatformSystemTrayIcon(QObject *parent)
137 : QObject(parent),
138 m_complete(false),
139 m_visible(false),
140 m_menu(nullptr),
141 m_iconLoader(nullptr),
142 m_handle(nullptr)
143{
144 m_handle = QGuiApplicationPrivate::platformTheme()->createPlatformSystemTrayIcon();
145 if (!m_handle)
146 m_handle = QWidgetPlatform::createSystemTrayIcon(this);
147 qCDebug(qtLabsPlatformTray) << "SystemTrayIcon ->" << m_handle;
148
149 if (m_handle) {
150 connect(m_handle, &QPlatformSystemTrayIcon::activated, this, &QQuickLabsPlatformSystemTrayIcon::activated);
151 connect(m_handle, &QPlatformSystemTrayIcon::messageClicked, this, &QQuickLabsPlatformSystemTrayIcon::messageClicked);
152 }
153}
154
156{
157 if (m_menu)
158 m_menu->setSystemTrayIcon(nullptr);
159 cleanup();
160 delete m_iconLoader;
161 m_iconLoader = nullptr;
162 delete m_handle;
163 m_handle = nullptr;
164}
165
167{
168 return m_handle;
169}
170
171/*!
172 \readonly
173 \qmlproperty bool Qt.labs.platform::SystemTrayIcon::available
174
175 This property holds whether the system tray is available.
176*/
178{
179 return m_handle && m_handle->isSystemTrayAvailable();
180}
181
182/*!
183 \readonly
184 \qmlproperty bool Qt.labs.platform::SystemTrayIcon::supportsMessages
185
186 This property holds whether the system tray icon supports notification messages.
187
188 \sa showMessage()
189*/
191{
192 return m_handle && m_handle->supportsMessages();
193}
194
195/*!
196 \qmlproperty bool Qt.labs.platform::SystemTrayIcon::visible
197
198 This property holds whether the system tray icon is visible.
199
200 The default value is \c false.
201*/
203{
204 return m_visible;
205}
206
208{
209 if (m_visible == visible)
210 return;
211
212 if (m_handle && m_complete) {
213 if (visible)
214 init();
215 else
216 cleanup();
217 }
218
219 m_visible = visible;
220 emit visibleChanged();
221}
222
223/*!
224 \qmlproperty string Qt.labs.platform::SystemTrayIcon::tooltip
225
226 This property holds the tooltip of the system tray icon.
227*/
229{
230 return m_tooltip;
231}
232
233void QQuickLabsPlatformSystemTrayIcon::setTooltip(const QString &tooltip)
234{
235 if (m_tooltip == tooltip)
236 return;
237
238 if (m_handle && m_complete)
239 m_handle->updateToolTip(tooltip);
240
241 m_tooltip = tooltip;
242 emit tooltipChanged();
243}
244
245/*!
246 \qmlproperty Menu Qt.labs.platform::SystemTrayIcon::menu
247
248 This property holds a menu for the system tray icon.
249*/
251{
252 return m_menu;
253}
254
256{
257 if (m_menu == menu)
258 return;
259
260 if (m_menu)
261 m_menu->setSystemTrayIcon(nullptr);
262
263 if (menu)
264 menu->setSystemTrayIcon(this);
265
266 if (m_handle && m_complete) {
267 if (menu && menu->create())
268 m_handle->updateMenu(menu->handle());
269 else
270 m_handle->updateMenu(nullptr);
271 }
272
273 m_menu = menu;
274 emit menuChanged();
275}
276
277/*!
278 \since Qt.labs.platform 1.1 (Qt 5.12)
279 \qmlproperty rect Qt.labs.platform::SystemTrayIcon::geometry
280
281 This property holds the geometry of the system tray icon.
282*/
284{
285 return m_handle ? m_handle->geometry() : QRect();
286}
287
288/*!
289 \since Qt.labs.platform 1.1 (Qt 5.12)
290 \qmlproperty url Qt.labs.platform::SystemTrayIcon::icon.source
291 \qmlproperty string Qt.labs.platform::SystemTrayIcon::icon.name
292 \qmlproperty bool Qt.labs.platform::SystemTrayIcon::icon.mask
293
294 This property holds the system tray icon.
295
296 \code
297 SystemTrayIcon {
298 icon.mask: true
299 icon.source: "qrc:/images/tray-icon.png"
300 }
301 \endcode
302*/
304{
305 if (!m_iconLoader)
306 return QQuickLabsPlatformIcon();
307
308 return m_iconLoader->icon();
309}
310
312{
313 if (iconLoader()->icon() == icon)
314 return;
315
317 emit iconChanged();
318}
319
320/*!
321 \qmlmethod void Qt.labs.platform::SystemTrayIcon::show()
322
323 Shows the system tray icon.
324*/
326{
327 setVisible(true);
328}
329
330/*!
331 \qmlmethod void Qt.labs.platform::SystemTrayIcon::hide()
332
333 Hides the system tray icon.
334*/
339
340/*!
341 \qmlmethod void Qt.labs.platform::SystemTrayIcon::showMessage(string title, string message, MessageIcon icon, int msecs)
342
343 Shows a system tray message with the given \a title, \a message and \a icon
344 for the time specified in \a msecs.
345
346 \note System tray messages are dependent on the system configuration and user preferences,
347 and may not appear at all. Therefore, it should not be relied upon as the sole means for providing
348 critical information.
349
350 \sa supportsMessages, messageClicked()
351*/
352void QQuickLabsPlatformSystemTrayIcon::showMessage(const QString &title, const QString &msg, QPlatformSystemTrayIcon::MessageIcon icon, int msecs)
353{
354 if (m_handle)
355 m_handle->showMessage(title, msg, QIcon(), icon, msecs);
356}
357
359{
360 if (!m_handle)
361 return;
362
363 m_handle->init();
364 if (m_menu && m_menu->create())
365 m_handle->updateMenu(m_menu->handle());
366 m_handle->updateToolTip(m_tooltip);
367 if (m_iconLoader)
368 m_iconLoader->setEnabled(true);
369}
370
372{
373 if (m_handle)
374 m_handle->cleanup();
375 if (m_iconLoader)
376 m_iconLoader->setEnabled(false);
377}
378
382
384{
385 m_complete = true;
386 if (m_visible)
387 init();
388}
389
391{
392 if (!m_iconLoader) {
394 static int slot = staticMetaObject.indexOfSlot("updateIcon()");
395 m_iconLoader = new QQuickLabsPlatformIconLoader(slot, that);
396 m_iconLoader->setEnabled(m_complete);
397 }
398 return m_iconLoader;
399}
400
401void QQuickLabsPlatformSystemTrayIcon::updateIcon()
402{
403 if (!m_handle || !m_iconLoader)
404 return;
405
406 const QRect oldGeometry = m_handle->geometry();
407
408 m_handle->updateIcon(m_iconLoader->toQIcon());
409
410 if (oldGeometry != m_handle->geometry())
411 emit geometryChanged();
412}
413
414QT_END_NAMESPACE
415
416#include "moc_qquicklabsplatformsystemtrayicon_p.cpp"
void setIcon(const QQuickLabsPlatformIcon &icon)
QQuickLabsPlatformIcon icon() const
bool operator==(const QQuickLabsPlatformIcon &other) const
QQuickLabsPlatformMenu * menu() const
\qmlproperty Menu Qt.labs.platform::SystemTrayIcon::menu
void hide()
\qmlmethod void Qt.labs.platform::SystemTrayIcon::hide()
bool isVisible() const
\qmlproperty bool Qt.labs.platform::SystemTrayIcon::visible
void showMessage(const QString &title, const QString &message, QPlatformSystemTrayIcon::MessageIcon iconType=QPlatformSystemTrayIcon::Information, int msecs=10000)
\qmlmethod void Qt.labs.platform::SystemTrayIcon::showMessage(string title, string message,...
void classBegin() override
Invoked after class creation, but before any properties have been set.
QQuickLabsPlatformIconLoader * iconLoader() const
bool isAvailable() const
\readonly \qmlproperty bool Qt.labs.platform::SystemTrayIcon::available
bool supportsMessages() const
\readonly \qmlproperty bool Qt.labs.platform::SystemTrayIcon::supportsMessages
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
void setIcon(const QQuickLabsPlatformIcon &icon)
QString tooltip() const
\qmlproperty string Qt.labs.platform::SystemTrayIcon::tooltip