QT实现TrayIcon

void MainWindow::initTrayIcon()
{
    trayIcon=new QSystemTrayIcon(this);
    appIcon =new QIcon(NMyUtils::getFileFullPath("Reminder.png"));
    trayIcon->setIcon(*appIcon);
    this->setWindowIcon(*appIcon);

    autoStartAction = new QAction(tr("AutoStart"),this);
    autoStartAction->setCheckable(true);
    if(NMyUtils::isAutoStart())
    {
        autoStartAction->setChecked(true);
    }
    connect(autoStartAction, SIGNAL(triggered()), this, SLOT(autoStart()));
    showHideAction = new QAction(tr("Show"),this);
    showHideAction->setCheckable(true);
    showHideAction->setChecked(true);
    connect(showHideAction, SIGNAL(triggered()), this, SLOT(showHide()));
    quitAction = new QAction(tr("Quit"),this);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(autoStartAction);
    trayIconMenu->addAction(showHideAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);
    trayIcon->setContextMenu(trayIconMenu);

    trayIcon->setToolTip(tr("Reminder"));
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
    trayIcon->show();
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*