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
    评论
以下是实现程序最小化后从任务转到右下角托盘Qt代码: ```cpp #include <QApplication> #include <QSystemTrayIcon> #include <QMenu> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建系统托盘图标 QSystemTrayIcon* trayIcon = new QSystemTrayIcon; // 设置托盘图标 trayIcon->setIcon(QIcon(":/images/icon.png")); // 创建菜单 QMenu* trayIconMenu = new QMenu; QAction* restoreAction = new QAction("Restore", trayIconMenu); QAction* quitAction = new QAction("Quit", trayIconMenu); trayIconMenu->addAction(restoreAction); trayIconMenu->addAction(quitAction); // 设置托盘菜单 trayIcon->setContextMenu(trayIconMenu); // 显示系统托盘图标 trayIcon->show(); // 最小化程序时隐藏主窗口,并显示托盘提示信息 app.setQuitOnLastWindowClosed(false); QObject::connect(&app, &QGuiApplication::applicationStateChanged, [=](Qt::ApplicationState state) { if (state == Qt::ApplicationMinimized) { trayIcon->show(); QApplication::activeWindow()->hide(); } }); // 点击托盘图标恢复主窗口 QObject::connect(trayIcon, &QSystemTrayIcon::activated, [=](QSystemTrayIcon::ActivationReason reason) { if (reason == QSystemTrayIcon::Trigger) { trayIcon->hide(); QApplication::activeWindow()->showNormal(); } }); // 点击托盘菜单中的 Restore 恢复主窗口 QObject::connect(restoreAction, &QAction::triggered, [=]() { trayIcon->hide(); QApplication::activeWindow()->showNormal(); }); // 点击托盘菜单中的 Quit 退出程序 QObject::connect(quitAction, &QAction::triggered, [=]() { trayIcon->hide(); qApp->quit(); }); return app.exec(); } ``` 需要注意的是,为了实现最小化时隐藏主窗口并显示系统托盘图标,需要在 `main()` 函数中添加以下代码: ```cpp // 最小化程序时隐藏主窗口,并显示托盘提示信息 app.setQuitOnLastWindowClosed(false); QObject::connect(&app, &QGuiApplication::applicationStateChanged, [=](Qt::ApplicationState state) { if (state == Qt::ApplicationMinimized) { trayIcon->show(); QApplication::activeWindow()->hide(); } }); ``` 另外,为了实现点击托盘图标恢复主窗口,需要在 `main()` 函数中添加以下代码: ```cpp // 点击托盘图标恢复主窗口 QObject::connect(trayIcon, &QSystemTrayIcon::activated, [=](QSystemTrayIcon::ActivationReason reason) { if (reason == QSystemTrayIcon::Trigger) { trayIcon->hide(); QApplication::activeWindow()->showNormal(); } }); ``` 此外,为了实现点击托盘菜单中的 Restore 恢复主窗口,需要在 `main()` 函数中添加以下代码: ```cpp // 点击托盘菜单中的 Restore 恢复主窗口 QObject::connect(restoreAction, &QAction::triggered, [=]() { trayIcon->hide(); QApplication::activeWindow()->showNormal(); }); ``` 最后,为了实现点击托盘菜单中的 Quit 退出程序,需要在 `main()` 函数中添加以下代码: ```cpp // 点击托盘菜单中的 Quit 退出程序 QObject::connect(quitAction, &QAction::triggered, [=]() { trayIcon->hide(); qApp->quit(); }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值