📄 qsystemtrayicon.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qsystemtrayicon.h"#include "qsystemtrayicon_p.h"#ifndef QT_NO_SYSTEMTRAYICON#include "qmenu.h"#include "qevent.h"#include "qpoint.h"#include "qlabel.h"#include "qpushbutton.h"#include "qpainterpath.h"#include "qpainter.h"#include "qstyle.h"#include "qgridlayout.h"#include "qapplication.h"#include "qdesktopwidget.h"#include "qbitmap.h"#include "private/qlabel_p.h"#include "qapplication.h"/*! \class QSystemTrayIcon \brief The QSystemTrayIcon class provides an icon for an application in the system tray. \since 4.2 \ingroup application \ingroup desktop Modern operating systems usually provide a special area on the desktop, called the \e{system tray} or \e{notification area}, where long-running applications can display icons and short messages. \image system-tray.png The system tray on Windows XP. The QSystemTrayIcon class can be used on the following platforms: \list \o All supported versions of Windows. \o All window managers for X11 that implement the \l{freedesktop.org} system tray specification, including recent versions of KDE and GNOME. \o All supported version of Mac OS X. QSystemTrayIcon::showMessage() requires Growl to display messages. \endlist To check whether a system tray is present on the user's desktop, call the QSystemTrayIcon::isSystemTrayAvailable() static function. To add a system tray entry, create a QSystemTrayIcon object, call setContextMenu() to provide a context menu for the icon, and call show() to make it visible in the system tray. Status notification messages ("balloon messages") can be displayed at any time using showMessage(). If the system tray is unavailable when a system tray icon is constructed, but becomes available later, QSystemTrayIcon will automatically add an entry for the application in the system tray if the icon is \l visible. The activated() signal is emitted when the user activates the icon. Only on X11, when a tooltip is requested, the QSystemTrayIcon receives a QHelpEvent of type QEvent::ToolTip. Additionally, the QSystemTrayIcon receives wheel events of type QEvent::Wheel. These are not supported on any other platform. \sa QDesktopServices, QDesktopWidget, {Desktop Integration}, {System Tray Icon Example}*//*! \enum QSystemTrayIcon::MessageIcon This enum describes the icon that is shown when a balloon message is displayed. \value NoIcon No icon is shown. \value Information An information icon is shown. \value Warning A standard warning icon is shown. \value Critical A critical warning icon is shown. \sa QMessageBox*//*! Constructs a QSystemTrayIcon object with the given \a parent. The icon is initially invisible. \sa visible*/QSystemTrayIcon::QSystemTrayIcon(QObject *parent): QObject(*new QSystemTrayIconPrivate(), parent){}/*! Constructs a QSystemTrayIcon object with the given \a icon and \a parent. The icon is initially invisible. \sa visible*/QSystemTrayIcon::QSystemTrayIcon(const QIcon &icon, QObject *parent): QObject(*new QSystemTrayIconPrivate(), parent){ setIcon(icon);}/*! Removes the icon from the system tray and frees all allocated resources.*/QSystemTrayIcon::~QSystemTrayIcon(){ Q_D(QSystemTrayIcon); d->remove_sys();}#ifndef QT_NO_MENU/*! Sets the specified \a menu to be the context menu for the system tray icon. The menu will pop up when the user requests the context menu for the system tray icon by clicking the mouse button. On Mac OS X, this is currenly converted to a NSMenu, so the aboutToHide() signal is not emitted. \note The system tray icon does not take ownership of the menu. You must ensure that it is deleted at the appropriate time by, for example, creating the menu with a suitable parent object.*/void QSystemTrayIcon::setContextMenu(QMenu *menu){ Q_D(QSystemTrayIcon); d->menu = menu; d->updateMenu_sys();}/*! Returns the current context menu for the system tray entry.*/QMenu* QSystemTrayIcon::contextMenu() const{ Q_D(const QSystemTrayIcon); return d->menu;}#endif // QT_NO_MENU/*! \property QSystemTrayIcon::icon \brief the system tray icon On Windows, the system tray icon size is 16x16; on X11, the preferred size is 22x22. The icon will be scaled to the appropriate size as necessary.*/void QSystemTrayIcon::setIcon(const QIcon &icon){ Q_D(QSystemTrayIcon); d->icon = icon; d->updateIcon_sys();}QIcon QSystemTrayIcon::icon() const{ Q_D(const QSystemTrayIcon); return d->icon;}/*! \property QSystemTrayIcon::toolTip \brief the tooltip for the system tray entry On some systems, the tooltip's length is limited. The tooltip will be truncated if necessary.*/void QSystemTrayIcon::setToolTip(const QString &tooltip){ Q_D(QSystemTrayIcon); d->toolTip = tooltip; d->updateToolTip_sys();}QString QSystemTrayIcon::toolTip() const{ Q_D(const QSystemTrayIcon); return d->toolTip;}/*! \fn void QSystemTrayIcon::show() Shows the icon in the system tray. \sa hide(), visible*//*! \fn void QSystemTrayIcon::hide() Hides the system tray entry. \sa show(), visible*//*! \since 4.3 Returns the geometry of the system tray icon in screen coordinates. \sa visible*/QRect QSystemTrayIcon::geometry() const{ Q_D(const QSystemTrayIcon); if (!d->visible) return QRect(); return d->geometry_sys();}/*! \property QSystemTrayIcon::visible \brief whether the system tray entry is visible Setting this property to true or calling show() makes the system tray icon visible; setting this property to false or calling hide() hides it.*/void QSystemTrayIcon::setVisible(bool visible){ Q_D(QSystemTrayIcon); if (visible == d->visible) return; if (d->icon.isNull() && visible) qWarning("QSystemTrayIcon::setVisible: No Icon set"); d->visible = visible; if (d->visible) d->install_sys(); else d->remove_sys();}bool QSystemTrayIcon::isVisible() const{ Q_D(const QSystemTrayIcon); return d->visible;}/*! \reimp*/bool QSystemTrayIcon::event(QEvent *e){#if defined(Q_WS_X11) if (e->type() == QEvent::ToolTip) { Q_D(QSystemTrayIcon); return d->sys->deliverToolTipEvent(e); }#endif return QObject::event(e);}/*! \enum QSystemTrayIcon::ActivationReason This enum describes the reason the system tray was activated. \value Unknown Unknown reason \value Context The context menu for the system tray entry was requested \value DoubleClick The system tray entry was double clicked \value Trigger The system tray entry was clicked \value MiddleClick The system tray entry was clicked with the middle mouse button \sa activated()*//*!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -