原创

QSystemTrayIcon增加系统托盘图标Demo

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://shazhenyu.blog.csdn.net/article/details/62227648

先看一下效果:


代码如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QCloseEvent>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    QSystemTrayIcon *m_myTrayIcon;
    QMenu *m_menu;
    QAction *m_miniSizeAction;
    QAction *m_maxSizeAction;
    QAction *m_restoreWinAction;
    QAction *m_quitAction;
private:
    void initTrayMenu();
    void initTrayIcon();
private slots:
    void iconActivated(QSystemTrayIcon::ActivationReason reason);
protected:
    void closeEvent(QCloseEvent *event);
};

#endif // MAINWINDOW_H

源文件:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initTrayMenu();
    initTrayIcon();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initTrayMenu()
{
    //这里&后面的字母是自己定义的
    m_miniSizeAction = new QAction("Min(&V)",this);
    m_maxSizeAction = new QAction("Max(&X)",this);
    m_restoreWinAction = new QAction("Reduction(&R)",this);
    m_quitAction = new QAction("Quit(&Q)",this);
    m_miniSizeAction->setIcon(QIcon(":/ooopic_1489563147.ico"));
    m_maxSizeAction->setIcon(QIcon(":/ooopic_1489563147.ico"));
    m_quitAction->setIcon(QIcon(":/ooopic_1489563147.ico"));
    m_menu = new QMenu((QWidget*)QApplication::desktop());
    m_menu->addAction(m_miniSizeAction);
    m_menu->addAction(m_maxSizeAction);
    m_menu->addAction(m_restoreWinAction);
    m_menu->addSeparator();     //加入一个分离符
    m_menu->addAction(m_quitAction);
    connect(m_miniSizeAction, SIGNAL(triggered()), this, SLOT(hide()));
    connect(m_maxSizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
    connect(m_restoreWinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
    connect(m_quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void MainWindow::initTrayIcon()
{
    //判断系统是否支持系统托盘图标
    if (!QSystemTrayIcon::isSystemTrayAvailable()) {
        return;
    }
    m_myTrayIcon = new QSystemTrayIcon(this);
    QIcon tmpIcon = QIcon(":/ooopic_1489563147.ico");
    m_myTrayIcon->setIcon(tmpIcon);   //设置图标图片
    setWindowIcon(tmpIcon);  //把图片设置到窗口上
    m_myTrayIcon->setToolTip("SystemTrayIcon V1.0");    //托盘时,鼠标放上去的提示信息
    m_myTrayIcon->showMessage("SystemTrayIcon","Hi,This is my trayIcon",QSystemTrayIcon::Information,10000);
    m_myTrayIcon->setContextMenu(m_menu);     //设置托盘上下文菜单
    m_myTrayIcon->show();
    connect(m_myTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}

void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason) {
    case QSystemTrayIcon::Trigger:
    case QSystemTrayIcon::DoubleClick:
        showNormal();
        break;
    case QSystemTrayIcon::MiddleClick:
        m_myTrayIcon->showMessage("SystemTrayIcon","Hi,This is my trayIcon",QSystemTrayIcon::Information,10000);
        break;
    default:
        break;
    }
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (m_myTrayIcon->isVisible()) {//最小化
        m_myTrayIcon->showMessage("SystemTrayIcon","Hi,This is my trayIcon",QSystemTrayIcon::Information,5000);
        hide();
        event->ignore();
    } else {
        event->accept();
    }
}


文章最后发布于: 2017-03-15 16:08:08
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览