Qt之任务栏系统托盘图标

转自  --》 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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值