QSystemTrayIcon 类

QSystemTrayIcon 类提供用于系统托盘的应用程序图标。 更多...

Header: #include <QSystemTrayIcon>
CMake: find_package(Qt6 COMPONENTS Widgets REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QObject

公共类型

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

特性

公共函数

QSystemTrayIcon (const QIcon & icon , QObject * parent = nullptr)
QSystemTrayIcon (QObject * parent = nullptr)
virtual ~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

公共槽

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)

信号

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

静态公共成员

重实现保护函数

virtual bool event (QEvent * e ) override

详细描述

现代操作系统通常在桌面提供特殊区域,称为 系统托盘 or 通知区域 ,在那里长时间运行应用程序可以显示图标和短消息。

The system tray on Windows XP.

QSystemTrayIcon 类可以用于下列平台:

要校验系统托盘是否呈现在用户桌面,调用 QSystemTrayIcon::isSystemTrayAvailable () 静态函数。

要添加系统托盘条目,创建 QSystemTrayIcon 对象,调用 setContextMenu () 为图标提供上下文菜单,和调用 show () 去使之在系统托盘中可见。状态通知消息 (气球消息) 可以被随时显示,使用 showMessage ().

系统托盘不可用,当构造系统托盘图标时,但稍后变为可用,QSystemTrayIcon 会为应用程序在系统任托盘中自动添加条目,若图标 visible .

activated () 信号被发射,当用户激活图标时。

仅在 X11,当请求工具提示时,QSystemTrayIcon 接收 QHelpEvent 的类型 QEvent::ToolTip 。此外,QSystemTrayIcon 接收滚轮事件为类型 QEvent::Wheel 。这些不被支持,在任何其它平台。

另请参阅 QDesktopServices , 桌面集成 ,和 系统托盘图标范例 .

成员类型文档编制

enum QSystemTrayIcon:: ActivationReason

此枚举描述系统托盘被激活的原因。

常量 描述
QSystemTrayIcon::Unknown 0 未知原因
QSystemTrayIcon::Context 1 用于系统托盘条目的上下文菜单被请求
QSystemTrayIcon::DoubleClick 2 系统托盘条目被双击。

注意: On macOS, a double click will only be emitted if no context menu is set, since the menu opens on mouse press

常量 描述
QSystemTrayIcon::Trigger 3 系统托盘条目被点击
QSystemTrayIcon::MiddleClick 4 系统托盘条目被点击,采用鼠标中键

另请参阅 activated ().

enum QSystemTrayIcon:: MessageIcon

此枚举描述展示图标,当显示气球消息时。

常量 描述
QSystemTrayIcon::NoIcon 0 没有展示图标。
QSystemTrayIcon::Information 1 展示信息图标。
QSystemTrayIcon::Warning 2 展示标准警告图标。
QSystemTrayIcon::Critical 3 展示严重警告图标。

另请参阅 QMessageBox .

特性文档编制

icon : QIcon

此特性保持系统托盘图标

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 (const QIcon & icon , QObject * parent = nullptr)

Constructs a QSystemTrayIcon object with the given icon and parent .

图标最初是不可见的。

另请参阅 visible .

QSystemTrayIcon:: QSystemTrayIcon ( QObject * parent = nullptr)

Constructs a QSystemTrayIcon object with the given parent .

图标最初是不可见的。

另请参阅 visible .

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

[slot] void QSystemTrayIcon:: hide ()

隐藏系统托盘条目。

另请参阅 show () 和 visible .

[signal] void QSystemTrayIcon:: messageClicked ()

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

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

另请参阅 activated ().

[slot] void QSystemTrayIcon:: show ()

在系统托盘展示图标。

另请参阅 hide () 和 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.

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

Has been turned into a slot in Qt 5.2.

另请参阅 show () 和 supportsMessages ().

[slot, since 5.9] 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 .

该函数在 Qt 5.9 引入。

[virtual] QSystemTrayIcon:: ~QSystemTrayIcon ()

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

QMenu *QSystemTrayIcon:: contextMenu () const

Returns the current context menu for the system tray entry.

另请参阅 setContextMenu ().

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

重实现: QObject::event (QEvent *e).

QRect QSystemTrayIcon:: geometry () const

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

另请参阅 visible .

[static] bool QSystemTrayIcon:: isSystemTrayAvailable ()

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

void QSystemTrayIcon:: setContextMenu ( QMenu * menu )

设置指定 menu 成为用于系统托盘图标的上下文菜单。

菜单会弹出,当用户通过点击鼠标按钮请求用于系统托盘图标的上下文菜单时。

On macOS, this is currenly converted to a NSMenu, so the aboutToHide() signal is not emitted.

注意: 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 ().

[static] bool QSystemTrayIcon:: supportsMessages ()

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

另请参阅 showMessage ().