1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \example desktop/systray
|
---|
44 | \title System Tray Icon Example
|
---|
45 |
|
---|
46 |
|
---|
47 | The System Tray Icon example shows how to add an icon with a menu
|
---|
48 | and popup messages to a desktop environment's system tray.
|
---|
49 |
|
---|
50 | \image systemtray-example.png Screenshot of the System Tray Icon.
|
---|
51 |
|
---|
52 | Modern operating systems usually provide a special area on the
|
---|
53 | desktop, called the system tray or notification area, where
|
---|
54 | long-running applications can display icons and short messages.
|
---|
55 |
|
---|
56 | This example consists of one single class, \c Window, providing
|
---|
57 | the main application window (i.e., an editor for the system tray
|
---|
58 | icon) and the associated icon.
|
---|
59 |
|
---|
60 | \image systemtray-editor.png
|
---|
61 |
|
---|
62 | The editor allows the user to choose the preferred icon as well as
|
---|
63 | set the balloon message's type and duration. The user can also
|
---|
64 | edit the message's title and body. Finally, the editor provide a
|
---|
65 | checkbox controlling whether the icon is actually shown in the
|
---|
66 | system tray, or not.
|
---|
67 |
|
---|
68 | \section1 Window Class Definition
|
---|
69 |
|
---|
70 | The \c Window class inherits QWidget:
|
---|
71 |
|
---|
72 | \snippet examples/desktop/systray/window.h 0
|
---|
73 |
|
---|
74 | We implement several private slots to respond to user
|
---|
75 | interaction. The other private functions are only convenience
|
---|
76 | functions provided to simplify the constructor.
|
---|
77 |
|
---|
78 | The tray icon is an instance of the QSystemTrayIcon class. To
|
---|
79 | check whether a system tray is present on the user's desktop, call
|
---|
80 | the static QSystemTrayIcon::isSystemTrayAvailable()
|
---|
81 | function. Associated with the icon, we provide a menu containing
|
---|
82 | the typical \gui minimize, \gui maximize, \gui restore and \gui
|
---|
83 | quit actions. We reimplement the QWidget::setVisible() function to
|
---|
84 | update the tray icon's menu whenever the editor's appearance
|
---|
85 | changes, e.g., when maximizing or minimizing the main application
|
---|
86 | window.
|
---|
87 |
|
---|
88 | Finally, we reimplement QWidget's \l {QWidget::}{closeEvent()}
|
---|
89 | function to be able to inform the user (when closing the editor
|
---|
90 | window) that the program will keep running in the system tray
|
---|
91 | until the user chooses the \gui Quit entry in the icon's context
|
---|
92 | menu.
|
---|
93 |
|
---|
94 | \section1 Window Class Implementation
|
---|
95 |
|
---|
96 | When constructing the editor widget, we first create the various
|
---|
97 | editor elements before we create the actual system tray icon:
|
---|
98 |
|
---|
99 | \snippet examples/desktop/systray/window.cpp 0
|
---|
100 |
|
---|
101 | We ensure that the application responds to user input by
|
---|
102 | connecting most of the editor's input widgets (including the
|
---|
103 | system tray icon) to the application's private slots. But note the
|
---|
104 | visibility checkbox; its \l {QCheckBox::}{toggled()} signal is
|
---|
105 | connected to the \e {icon}'s \l {QSystemTrayIcon::}{setVisible()}
|
---|
106 | function instead.
|
---|
107 |
|
---|
108 | \snippet examples/desktop/systray/window.cpp 3
|
---|
109 |
|
---|
110 | The \c setIcon() slot is triggered whenever the current index in
|
---|
111 | the icon combobox changes, i.e., whenever the user chooses another
|
---|
112 | icon in the editor. Note that it is also called when the user
|
---|
113 | activates the tray icon with the left mouse button, triggering the
|
---|
114 | icon's \l {QSystemTrayIcon::}{activated()} signal. We will come
|
---|
115 | back to this signal shortly.
|
---|
116 |
|
---|
117 | The QSystemTrayIcon::setIcon() function sets the \l
|
---|
118 | {QSystemTrayIcon::}{icon} property that holds the actual system
|
---|
119 | tray icon. On Windows, the system tray icon size is 16x16; on X11,
|
---|
120 | the preferred size is 22x22. The icon will be scaled to the
|
---|
121 | appropriate size as necessary.
|
---|
122 |
|
---|
123 | Note that on X11, due to a limitation in the system tray
|
---|
124 | specification, mouse clicks on transparent areas in the icon are
|
---|
125 | propagated to the system tray. If this behavior is unacceptable,
|
---|
126 | we suggest using an icon with no transparency.
|
---|
127 |
|
---|
128 | \snippet examples/desktop/systray/window.cpp 4
|
---|
129 |
|
---|
130 | Whenever the user activates the system tray icon, it emits its \l
|
---|
131 | {QSystemTrayIcon::}{activated()} signal passing the triggering
|
---|
132 | reason as parameter. QSystemTrayIcon provides the \l
|
---|
133 | {QSystemTrayIcon::}{ActivationReason} enum to describe how the
|
---|
134 | icon was activated.
|
---|
135 |
|
---|
136 | In the constructor, we connected our icon's \l
|
---|
137 | {QSystemTrayIcon::}{activated()} signal to our custom \c
|
---|
138 | iconActivated() slot: If the user has clicked the icon using the
|
---|
139 | left mouse button, this function changes the icon image by
|
---|
|
---|