转自 --》 http://blog.csdn.net/qivan/article/details/7506306
托盘图标,一个自己脑子出现很久的词,可惜自己都没动手去实现。最近看见的,听见的多了,自己也感兴趣就弄弄了,感觉还蛮简单了。
贴出效果图:
那么多功能,其实就一个类就搞定了,那就是QSystemTrayIcon
头文件(主要 1.声明菜单相关动作 2.声明系统托盘对象,以及相关托盘槽函数 3.关闭事件)
#include <QDialog>
#include <QtGui>
class SystemTrayIcon : public QDialog
{
Q_OBJECT
public:
explicit SystemTrayIcon(QWidget *parent = 0);
void CreatTrayMenu();
void CreatTrayIcon();
QSystemTrayIcon *myTrayIcon;
QMenu *myMenu;
QAction *miniSizeAction;
QAction *maxSizeAction;
QAction *restoreWinAction;
QAction *quitAction;
protected:
void closeEvent(QCloseEvent *event);
signals:
public slots:
void iconActivated(QSystemTrayIcon::ActivationReason reason);
};
#endif // SYSTEMTRAYICON_H
源文件(1.创建上下文菜单 2.创建系统托盘,实现相关功能)
#include "systemtrayicon.h"
SystemTrayIcon::SystemTrayIcon(QWidget *parent) :
QDialog(parent)
{
CreatTrayIcon();
}
void SystemTrayIcon::CreatTrayMenu()
{
miniSizeAction = new QAction("最小化(&N)",this);
maxSizeAction = new QAction("最大化(&X)",this);
restoreWinAction = new QAction("还 原(&R)",this);
quitAction = new QAction("退出(&Q)",this);
miniSizeAction->setIcon(QIcon(":/img/2.png"));
maxSizeAction->setIcon(QIcon(":/img/3.png.png"));
restoreWinAction->setIcon(QIcon(":/img/4.png.png"));
quitAction->setIcon(QIcon(":/img/5.png.png"));
this->connect(miniSizeAction,SIGNAL(triggered()),this,SLOT(hide()));
this->connect(maxSizeAction,SIGNAL(triggered()),this,SLOT(showMaximized()));
this->connect(restoreWinAction,SIGNAL(triggered()),this,SLOT(showNormal()));
this->connect(quitAction,SIGNAL(triggered()),qApp,SLOT(quit()));
myMenu = new QMenu((QWidget*)QApplication::desktop());
myMenu->addAction(miniSizeAction);
myMenu->addAction(maxSizeAction);
myMenu->addAction(restoreWinAction);
myMenu->addSeparator(); //加入一个分离符
myMenu->addAction(quitAction);
}
void SystemTrayIcon::CreatTrayIcon()
{
CreatTrayMenu();
if (!QSystemTrayIcon::isSystemTrayAvailable()) //判断系统是否支持系统托盘图标
{
return;
}
myTrayIcon = new QSystemTrayIcon(this);
myTrayIcon->setIcon(QIcon(":/img/2.png")); //设置图标图片
//setWindowIcon(QIcon(":/img/2.png")); //把图片设置到窗口上
myTrayIcon->setToolTip("SystemTrayIcon V1.0"); //托盘时,鼠标放上去的提示信息
myTrayIcon->showMessage("SystemTrayIcon","Hi,This is my trayIcon",QSystemTrayIcon::Information,10000);
myTrayIcon->setContextMenu(myMenu); //设置托盘上下文菜单
myTrayIcon->show();
this->connect(myTrayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
void SystemTrayIcon::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
showNormal();
break;
case QSystemTrayIcon::MiddleClick:
myTrayIcon->showMessage("SystemTrayIcon","Hi,This is my trayIcon",QSystemTrayIcon::Information,10000);
break;
default:
break;
}
}
void SystemTrayIcon::closeEvent(QCloseEvent *event)
{
if (myTrayIcon->isVisible())
{
myTrayIcon->showMessage("SystemTrayIcon","Hi,This is my trayIcon",QSystemTrayIcon::Information,5000);
hide(); //最小化
event->ignore();
}
else
event->accept();
}