QSystemTrayIcon Class

The QSystemTrayIcon class provides an icon for an application in the system tray. 更多...

头文件: #include <QSystemTrayIcon>
qmake: QT += widgets
开始支持版本: Qt 4.2
基类: QObject

公有类型

enum ActivationReason { Unknown, Context, DoubleClick, Trigger, MiddleClick }
enum MessageIcon { NoIcon, Information, Warning, Critical }

属性

公有函数

QSystemTrayIcon(QObject *parent = Q_NULLPTR)
QSystemTrayIcon(const QIcon &icon, QObject *parent = Q_NULLPTR)
~QSystemTrayIcon()
QMenu *contextMenu() const
QRect geometry() const
QIcon icon() const
bool isVisible() const
void setContextMenu(QMenu *menu)
void setIcon(const QIcon &icon)
void setToolTip(const QString &tip)
QString toolTip() const
  • 32 个公有函数继承自 QObject

公有槽函数

void hide()
void setVisible(bool visible)
void show()
void showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information, int millisecondsTimeoutHint = 10000)
void showMessage(const QString &title, const QString &message, const QIcon &icon, int millisecondsTimeoutHint = 10000)
  • 1 个公有槽函数继承自 QObject

信号

void activated(QSystemTrayIcon::ActivationReason reason)
void messageClicked()

静态公有成员

  • 10 个静态公有成员继承自 QObject

重新实现的受保护函数

virtual bool event(QEvent *e)
  • 9 个受保护的函数继承自 QObject

其他继承的成员

  • 1 个公有变量继承自 QObject
  • 9 个受保护的函数继承自 QObject
  • 2 个受保护的变量继承自 QObject

详细描述

The QSystemTrayIcon class provides an icon for an application in the system tray.

Modern operating systems usually provide a special area on the desktop, called the system tray or notification area, where long-running applications can display icons and short messages.

The system tray on Windows XP.

The QSystemTrayIcon class can be used on the following platforms:

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 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.

参见 QDesktopServices, QDesktopWidget, Desktop Integration, and System Tray Icon Example.

成员类型

enum QSystemTrayIcon::ActivationReason

This enum describes the reason the system tray was activated.

ConstantValueDescription
QSystemTrayIcon::Unknown0Unknown reason
QSystemTrayIcon::Context1The context menu for the system tray entry was requested
QSystemTrayIcon::DoubleClick2The system tray entry was double clicked
QSystemTrayIcon::Trigger3The system tray entry was clicked
QSystemTrayIcon::MiddleClick4The system tray entry was clicked with the middle mouse button

参见 activated().

enum QSystemTrayIcon::MessageIcon

This enum describes the icon that is shown when a balloon message is displayed.

ConstantValueDescription
QSystemTrayIcon::NoIcon0No icon is shown.
QSystemTrayIcon::Information1An information icon is shown.
QSystemTrayIcon::Warning2A standard warning icon is shown.
QSystemTrayIcon::Critical3A critical warning icon is shown.

参见 QMessageBox.

属性

icon : QIcon

This property holds 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.

访问函数:

QIcon icon() const
void setIcon(const QIcon &icon)

toolTip : QString

This property holds the tooltip for the system tray entry

On some systems, the tooltip's length is limited. The tooltip will be truncated if necessary.

访问函数:

QString toolTip() const
void setToolTip(const QString &tip)

visible : bool

This property holds 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.

访问函数:

bool isVisible() const
void setVisible(bool visible)

成员函数

QSystemTrayIcon::QSystemTrayIcon(QObject *parent = Q_NULLPTR)

Constructs a QSystemTrayIcon object with the given parent.

The icon is initially invisible.

参见 visible.

QSystemTrayIcon::QSystemTrayIcon(const QIcon &icon, QObject *parent = Q_NULLPTR)

Constructs a QSystemTrayIcon object with the given icon and parent.

The icon is initially invisible.

参见 visible.

QSystemTrayIcon::~QSystemTrayIcon()

Removes the icon from the system tray and frees all allocated resources.

[signal] void QSystemTrayIcon::activated(QSystemTrayIcon::ActivationReason reason)

This signal is emitted when the user activates the system tray icon. reason specifies the reason for activation. QSystemTrayIcon::ActivationReason enumerates the various reasons.

参见 QSystemTrayIcon::ActivationReason.

QMenu *QSystemTrayIcon::contextMenu() const

Returns the current context menu for the system tray entry.

参见 setContextMenu().

[virtual protected] bool QSystemTrayIcon::event(QEvent *e)

重新实现 QObject::event().

QRect QSystemTrayIcon::geometry() const

Returns the geometry of the system tray icon in screen coordinates.

This function was introduced in Qt 4.3.

参见 visible.

[slot] void QSystemTrayIcon::hide()

Hides the system tray entry.

参见 show() and visible.

[static] bool QSystemTrayIcon::isSystemTrayAvailable()

Returns true if the system tray is available; otherwise returns false.

If the system tray is currently unavailable but becomes available later, QSystemTrayIcon will automatically add an entry in the system tray if it is visible.

[signal] void QSystemTrayIcon::messageClicked()

This signal is emitted when the message displayed using showMessage() was clicked by the user.

Currently this signal is not sent on macOS.

Note: We follow Microsoft Windows XP/Vista behavior, so the signal is also emitted when the user clicks on a tray icon with a balloon message displayed.

参见 activated().

void QSystemTrayIcon::setContextMenu(QMenu *menu)

Sets the specified 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 macOS, 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.

参见 contextMenu().

[slot] void QSystemTrayIcon::show()

Shows the icon in the system tray.

参见 hide() and visible.

[slot] void QSystemTrayIcon::showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information, int millisecondsTimeoutHint = 10000)

Shows a balloon message for the entry with the given title, message and icon for the time specified in millisecondsTimeoutHint. title and message must be plain text strings.

Message can be clicked by the user; the messageClicked() signal will emitted when this occurs.

Note that display of messages are dependent on the system configuration and user preferences, and that messages may not appear at all. Hence, it should not be relied upon as the sole means for providing critical information.

On Windows, the millisecondsTimeoutHint is usually ignored by the system when the application has focus.

On macOS, the Growl notification system must be installed for this function to display messages.

Has been turned into a slot in Qt 5.2.

This function was introduced in Qt 4.3.

参见 show() and supportsMessages().

[slot] void QSystemTrayIcon::showMessage(const QString &title, const QString &message, const QIcon &icon, int millisecondsTimeoutHint = 10000)

This function overloads showMessage().

Shows a balloon message for the entry with the given title, message, and custom icon icon for the time specified in millisecondsTimeoutHint.

This function was introduced in Qt 5.9.

[static] bool QSystemTrayIcon::supportsMessages()

Returns true if the system tray supports balloon messages; otherwise returns false.

参见 showMessage().