Qt实现系统托盘 托盘菜单 托盘提示 托盘闪烁 任务栏提示 

Qt实现系统托盘 托盘菜单 托盘提示 托盘闪烁 任务栏提示 

SystemTrayIcon.pro

#-------------------------------------------------
#
# Project created by QtCreator 2020-02-29T14:09:07
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = SystemTrayIcon
TEMPLATE = app


SOURCES += main.cpp\
        MainWindow.cpp

HEADERS  += MainWindow.h

FORMS    += MainWindow.ui

RESOURCES += \
    images.qrc

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QEvent>

#include <QSystemTrayIcon>
class QAction;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void closeEvent(QCloseEvent *event);

private:
    void createSystemTray();

private slots:
    void onActivatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason);
    void onTimerOut();
    void onFlashTimerOut();

private:
    Ui::MainWindow *ui;
    QSystemTrayIcon         *systemTray;
    QAction                 *minimumAct;
    QAction                 *maximumAct;
    QAction                 *quitAct;
    QAction                 *restoreAct;

    QTimer                  *m_pTimerNotice;
    QTimer                  *m_pTimerFlash;
    uint                     m_flashCount;
};

#endif // MAINWINDOW_H

MainWindow.cpp

#include "MainWindow.h"
#include "ui_MainWindow.h"

#include <QDebug>
#include <QCloseEvent>
#include <QTimer>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow), m_pTimerFlash(NULL)
{
    ui->setupUi(this);

    createSystemTray();
}

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

void MainWindow::closeEvent(QCloseEvent *event)
{
    if(systemTray->isVisible())
    {
        hide();
        systemTray->showMessage(tr("Tray"), tr("The programe is running!"), QSystemTrayIcon::Warning, 15000);
    }
    event->ignore();
}

void MainWindow::createSystemTray()
{
    QIcon icon(":/app/res/images/appicon.ico");
    systemTray = new QSystemTrayIcon(this);
    systemTray->setIcon(icon);
    systemTray->setToolTip(tr("Tray Test"));
    minimumAct = new QAction(QIcon(":/app/res/images/min_window.ico"), tr("Min"), this);
    connect(minimumAct, SIGNAL(triggered()), this, SLOT(hide()));
    maximumAct = new QAction(QIcon(":/app/res/images/max_window.ico"), tr("Max"), this);
    connect(maximumAct, SIGNAL(triggered()), this, SLOT(showMaximized()));
    restoreAct = new QAction(QIcon(":/app/res/images/restore_window.ico"), tr("Restore"), this);
    connect(restoreAct, SIGNAL(triggered()), this, SLOT(showNormal()));
    quitAct = new QAction(QIcon(":/app/res/images/exit.ico"), tr("Exit"), this);
    connect(quitAct, SIGNAL(triggered()), qApp, SLOT(quit()));

    connect(systemTray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(onActivatedSysTrayIcon(QSystemTrayIcon::ActivationReason)));

    QMenu *pContextMenu = new QMenu(this);
    pContextMenu->addAction(minimumAct);
    pContextMenu->addAction(maximumAct);
    pContextMenu->addAction(restoreAct);
    pContextMenu->addSeparator();
    pContextMenu->addAction(quitAct);

    systemTray->setContextMenu(pContextMenu);
    systemTray->show();

    // 此处使用定时器模拟消息的发送
    m_pTimerNotice = new QTimer;
    connect(m_pTimerNotice, &QTimer::timeout, this, &MainWindow::onTimerOut);
    m_pTimerNotice->start(5000);
}

void MainWindow::onActivatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason){
    case QSystemTrayIcon::Trigger:
        m_pTimerNotice->stop();
        delete m_pTimerFlash;
        m_pTimerFlash = NULL;
        systemTray->setIcon(QIcon(":/app/res/images/appicon.ico"));
        show();
        break;
    default:
        break;
    }
}

void MainWindow::onTimerOut()
{
    QApplication::alert(this);// 任务栏通知
    m_flashCount = 0;
    if (m_pTimerFlash == NULL)
    {
        m_pTimerFlash = new QTimer;
        connect(m_pTimerFlash, &QTimer::timeout, this, &MainWindow::onFlashTimerOut);
        m_pTimerFlash->start(500);
    }
}

void MainWindow::onFlashTimerOut()
{
    // 消息通知,使用定时器刷新图标
    m_flashCount++;
    if (m_flashCount % 2 == 1)
    {
        systemTray->setIcon(QIcon(":/app/res/images/notice.ico"));//(或者为空)
    }
    else
    {
        systemTray->setIcon(QIcon(":/app/res/images/appicon.ico"));
    }
}

main.cpp

#include "MainWindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值