系统托盘图标是一种很有用的提示方式,Qt作为一款强大的GUI设计工具自然不会在这个功能上缺席。
在qt4的demo中详细的记录了系统托盘图标的使用方法,经过实际的操作我也在自己的程序里使用了一把。在这里记录一下。
使用的类是QSystemTrayIcon(引用一下帮助文档里的介绍)
这里需要值得注意的是
The QSystemTrayIcon class can be used on the following platforms:
- All supported versions of Windows.
- All window managers for X11 that implement the freedesktop.org system tray specification, including recent versions of KDE and GNOME.
- All supported versions of Mac OS X. Note that the Growl notification system must be installed forQSystemTrayIcon::showMessage() to display messages.
其他的详细内容可以通过帮助手册来查询。
使用的方法和步骤如下:
1、#include <QSystemTrayIcon>如果需要系统托盘图标的菜单,则还需要#include <QMenu>
2、声明对象QSystemTrayIcon *tray和QMenu *trayMenu;
3、接下来可以对tay进行一些初始化的工作(部分代码省略,可以参照手册)
tray->setToolTip(...);//设置tooltip
tray->setIcon(QIcon("..."));//设置显示的图标,这里QIcon类不需要单独声明
tray->show();//在系统托盘显示图标,可以更具需要在适当时候显示图标
到此,系统托盘图标已经可以成功的显示在系统托盘了,接下来可以用tray->showMessage来显示一些消息。
后面的内容再记录其他的一些功能。
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.