QT 创建托盘图标和右键菜单

void Shutdown::initTrayIcon()    
{    
        //创建并设置动作    
    minAction = new QAction(this);    
    minAction->setIcon(QIcon(":/icons/min.bmp"));    
    connect(minAction,SIGNAL(triggered()),this,SLOT(hide()));

  

    restoreAction = new QAction(this);    
    restoreAction->setIcon(QIcon(":/icons/restore.bmp"));    
    connect(restoreAction,SIGNAL(triggered()),this,SLOT(showNormal()));

  

    aboutAction = new QAction(this);    
    aboutAction->setIcon(QIcon(":/icons/shutdown03.jpg"));    
    connect(aboutAction,SIGNAL(triggered()),this,SLOT(showAboutDlg()));

  

    quitAction = new QAction(this);    
    quitAction->setIcon(QIcon(":/icons/quit.bmp"));    
    connect(quitAction,SIGNAL(triggered()),qApp,SLOT(quit()));

  

    //创建托盘图标菜单并添加动作    
    trayIconMenu = new QMenu(this);    
    trayIconMenu->addAction(restoreAction);    
    trayIconMenu->addAction(minAction);    
    trayIconMenu->addSeparator();    
    trayIconMenu->addAction(aboutAction);    
    trayIconMenu->addSeparator();    
    trayIconMenu->addAction(quitAction);

  

    //创建并设置托盘图标    
    trayIcon = new QSystemTrayIcon(this);    
    trayIcon->setContextMenu(trayIconMenu);    
    trayIcon->setIcon(QIcon(":/icons/shutdown01.png"));

  

    //显示系统托盘图标    
    trayIcon->show();

  

    connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,    
            SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));    
    connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(trayIconMsgClicked()));    
}

你可能感兴趣的:(QT)