Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
5
#
include
"qquicklabsplatformsystemtrayicon_p.h"
6
#
include
"qquicklabsplatformmenu_p.h"
7
#
include
"qquicklabsplatformiconloader_p.h"
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
15
QT_BEGIN_NAMESPACE
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
134
Q_STATIC_LOGGING_CATEGORY(qtLabsPlatformTray,
"qt.labs.platform.tray"
)
135
136
QQuickLabsPlatformSystemTrayIcon::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
155
QQuickLabsPlatformSystemTrayIcon
::~
QQuickLabsPlatformSystemTrayIcon
()
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
166
QPlatformSystemTrayIcon
*
QQuickLabsPlatformSystemTrayIcon
::
handle
()
const
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
*/
177
bool
QQuickLabsPlatformSystemTrayIcon
::
isAvailable
()
const
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
*/
190
bool
QQuickLabsPlatformSystemTrayIcon
::
supportsMessages
()
const
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
*/
202
bool
QQuickLabsPlatformSystemTrayIcon
::
isVisible
()
const
203
{
204
return
m_visible;
205
}
206
207
void
QQuickLabsPlatformSystemTrayIcon
::
setVisible
(
bool
visible)
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
*/
228
QString
QQuickLabsPlatformSystemTrayIcon
::
tooltip
()
const
229
{
230
return
m_tooltip;
231
}
232
233
void
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
*/
250
QQuickLabsPlatformMenu
*
QQuickLabsPlatformSystemTrayIcon
::
menu
()
const
251
{
252
return
m_menu;
253
}
254
255
void
QQuickLabsPlatformSystemTrayIcon
::
setMenu
(
QQuickLabsPlatformMenu
*menu)
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
*/
283
QRect
QQuickLabsPlatformSystemTrayIcon
::
geometry
()
const
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
*/
303
QQuickLabsPlatformIcon
QQuickLabsPlatformSystemTrayIcon
::
icon
()
const
304
{
305
if
(!m_iconLoader)
306
return
QQuickLabsPlatformIcon
();
307
308
return
m_iconLoader
->
icon
(
)
;
309
}
310
311
void
QQuickLabsPlatformSystemTrayIcon
::
setIcon
(
const
QQuickLabsPlatformIcon
&icon)
312
{
313
if
(
iconLoader
(
)
->
icon
(
)
==
icon)
314
return
;
315
316
iconLoader
(
)
->
setIcon
(
icon
)
;
317
emit iconChanged();
318
}
319
320
/*!
321
\qmlmethod void Qt.labs.platform::SystemTrayIcon::show()
322
323
Shows the system tray icon.
324
*/
325
void
QQuickLabsPlatformSystemTrayIcon
::show()
326
{
327
setVisible
(
true
)
;
328
}
329
330
/*!
331
\qmlmethod void Qt.labs.platform::SystemTrayIcon::hide()
332
333
Hides the system tray icon.
334
*/
335
void
QQuickLabsPlatformSystemTrayIcon
::
hide
()
336
{
337
setVisible
(
false
)
;
338
}
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
*/
352
void
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
358
void
QQuickLabsPlatformSystemTrayIcon
::
init
()
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
371
void
QQuickLabsPlatformSystemTrayIcon
::
cleanup
()
372
{
373
if
(m_handle)
374
m_handle->cleanup();
375
if
(m_iconLoader)
376
m_iconLoader
->
setEnabled
(
false
)
;
377
}
378
379
void
QQuickLabsPlatformSystemTrayIcon
::
classBegin
()
380
{
381
}
382
383
void
QQuickLabsPlatformSystemTrayIcon
::
componentComplete
()
384
{
385
m_complete =
true
;
386
if
(m_visible)
387
init
(
)
;
388
}
389
390
QQuickLabsPlatformIconLoader
*
QQuickLabsPlatformSystemTrayIcon
::
iconLoader
()
const
391
{
392
if
(!m_iconLoader) {
393
QQuickLabsPlatformSystemTrayIcon
*that =
const_cast
<
QQuickLabsPlatformSystemTrayIcon
*>(
this
);
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
401
void
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
414
QT_END_NAMESPACE
415
416
#
include
"moc_qquicklabsplatformsystemtrayicon_p.cpp"
QQuickLabsPlatformIconLoader
Definition
qquicklabsplatformiconloader_p.h:31
QQuickLabsPlatformIconLoader::setIcon
void setIcon(const QQuickLabsPlatformIcon &icon)
Definition
qquicklabsplatformiconloader.cpp:46
QQuickLabsPlatformIconLoader::icon
QQuickLabsPlatformIcon icon() const
Definition
qquicklabsplatformiconloader.cpp:41
QQuickLabsPlatformIconLoader::setEnabled
void setEnabled(bool enabled)
Definition
qquicklabsplatformiconloader.cpp:26
QQuickLabsPlatformIcon
Definition
qquicklabsplatformicon_p.h:30
QQuickLabsPlatformIcon::operator==
bool operator==(const QQuickLabsPlatformIcon &other) const
Definition
qquicklabsplatformicon.cpp:39
QQuickLabsPlatformMenu
Definition
qquicklabsplatformmenu_p.h:44
QQuickLabsPlatformSystemTrayIcon
Definition
qquicklabsplatformsystemtrayicon_p.h:35
QQuickLabsPlatformSystemTrayIcon::~QQuickLabsPlatformSystemTrayIcon
~QQuickLabsPlatformSystemTrayIcon()
Definition
qquicklabsplatformsystemtrayicon.cpp:155
QQuickLabsPlatformSystemTrayIcon::menu
QQuickLabsPlatformMenu * menu() const
\qmlproperty Menu Qt.labs.platform::SystemTrayIcon::menu
Definition
qquicklabsplatformsystemtrayicon.cpp:250
QQuickLabsPlatformSystemTrayIcon::hide
void hide()
\qmlmethod void Qt.labs.platform::SystemTrayIcon::hide()
Definition
qquicklabsplatformsystemtrayicon.cpp:335
QQuickLabsPlatformSystemTrayIcon::isVisible
bool isVisible() const
\qmlproperty bool Qt.labs.platform::SystemTrayIcon::visible
Definition
qquicklabsplatformsystemtrayicon.cpp:202
QQuickLabsPlatformSystemTrayIcon::showMessage
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,...
Definition
qquicklabsplatformsystemtrayicon.cpp:352
QQuickLabsPlatformSystemTrayIcon::icon
QQuickLabsPlatformIcon icon() const
Definition
qquicklabsplatformsystemtrayicon.cpp:303
QQuickLabsPlatformSystemTrayIcon::classBegin
void classBegin() override
Invoked after class creation, but before any properties have been set.
Definition
qquicklabsplatformsystemtrayicon.cpp:379
QQuickLabsPlatformSystemTrayIcon::init
void init()
Definition
qquicklabsplatformsystemtrayicon.cpp:358
QQuickLabsPlatformSystemTrayIcon::handle
QPlatformSystemTrayIcon * handle() const
Definition
qquicklabsplatformsystemtrayicon.cpp:166
QQuickLabsPlatformSystemTrayIcon::setTooltip
void setTooltip(const QString &tooltip)
Definition
qquicklabsplatformsystemtrayicon.cpp:233
QQuickLabsPlatformSystemTrayIcon::setVisible
void setVisible(bool visible)
Definition
qquicklabsplatformsystemtrayicon.cpp:207
QQuickLabsPlatformSystemTrayIcon::iconLoader
QQuickLabsPlatformIconLoader * iconLoader() const
Definition
qquicklabsplatformsystemtrayicon.cpp:390
QQuickLabsPlatformSystemTrayIcon::geometry
QRect geometry() const
Definition
qquicklabsplatformsystemtrayicon.cpp:283
QQuickLabsPlatformSystemTrayIcon::setMenu
void setMenu(QQuickLabsPlatformMenu *menu)
Definition
qquicklabsplatformsystemtrayicon.cpp:255
QQuickLabsPlatformSystemTrayIcon::isAvailable
bool isAvailable() const
\readonly \qmlproperty bool Qt.labs.platform::SystemTrayIcon::available
Definition
qquicklabsplatformsystemtrayicon.cpp:177
QQuickLabsPlatformSystemTrayIcon::supportsMessages
bool supportsMessages() const
\readonly \qmlproperty bool Qt.labs.platform::SystemTrayIcon::supportsMessages
Definition
qquicklabsplatformsystemtrayicon.cpp:190
QQuickLabsPlatformSystemTrayIcon::componentComplete
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
Definition
qquicklabsplatformsystemtrayicon.cpp:383
QQuickLabsPlatformSystemTrayIcon::cleanup
void cleanup()
Definition
qquicklabsplatformsystemtrayicon.cpp:371
QQuickLabsPlatformSystemTrayIcon::setIcon
void setIcon(const QQuickLabsPlatformIcon &icon)
Definition
qquicklabsplatformsystemtrayicon.cpp:311
QQuickLabsPlatformSystemTrayIcon::tooltip
QString tooltip() const
\qmlproperty string Qt.labs.platform::SystemTrayIcon::tooltip
Definition
qquicklabsplatformsystemtrayicon.cpp:228
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtdeclarative
src
labs
platform
qquicklabsplatformsystemtrayicon.cpp
Generated on
for Qt by
1.18.0