QSystemTrayIcon 系统托盘使用总结
阅读原文时间:2021年04月23日阅读:1

main.cpp

#include "TestLCD.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TestLCD w;
    w.show();
    return a.exec();
}

testLCD.h

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_TestLCD.h"
#include <QSystemTrayIcon>

class TestLCD : public QMainWindow
{
    Q_OBJECT

public:
    TestLCD(QWidget *parent = Q_NULLPTR);

private:
    void initTrayIcon();
    void showWindow();

protected slots:

    void onActivated(QSystemTrayIcon::ActivationReason reason);
    void QuitApp();


private:
    QButtonGroup *m_pButtonGroup;
    bool pointVisible;
    QLCDNumber *m_pLCD;
    Ui::TestLCDClass ui;
};

testLCD.cpp

#include "TestLCD.h"
#include "TrayMenu.h"

TestLCD::TestLCD(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    initTrayIcon();
}
void TestLCD::initTrayIcon()
{
    QSystemTrayIcon *pSystemTray = new QSystemTrayIcon(this);
    TrayMenu *pTrayMenu = new TrayMenu(this);

    // 设置系统托盘的上下文菜单
    pSystemTray->setContextMenu(pTrayMenu);

    // 设置系统托盘提示信息、托盘图标
    pSystemTray->setToolTip(QString::fromLocal8Bit("我就是托盘"));
    pSystemTray->setIcon(QIcon("src/Me.png"));

    // 连接信号槽
    connect(pTrayMenu, SIGNAL(showWindow()), this, SLOT(showWindow()));
    connect(pTrayMenu, SIGNAL(QuitApp()), this, SLOT(QuitApp()));
    connect(pSystemTray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(onActivated(QSystemTrayIcon::ActivationReason)));

    // 显示系统托盘
    pSystemTray->show();

    // 显示系统托盘提示信息
    pSystemTray->showMessage(QString::fromLocal8Bit("托盘标题"), QString::fromLocal8Bit("托盘显示内容"));
}

void TestLCD::showWindow()
{
    showNormal();
    raise();
    activateWindow();
}

void TestLCD::onActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason)
    {
        // 单击托盘显示窗口
    case QSystemTrayIcon::Trigger:
    {
        showWindow();
        break;
    }
    // 双击
    case QSystemTrayIcon::DoubleClick:
    {
        // ...
        break;
    }
    default:
        break;
    }
}

void TestLCD::QuitApp()
{
    exit(0);
}

TrayMenue.h

#pragma once

#include <QMenu>

class TrayMenu : public QMenu
{
    Q_OBJECT

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

signals:
    // 自定义信号
    void showWindow();
    void QuitApp();

private:
    // 添加菜单项
    void initActions();

private:
    QAction *m_pShowAction;
    QAction *m_pSettingAction;
    QAction *m_pHomePageAction;
    QAction *m_pHelpAction;
    QAction *m_pAboutAction;
    QAction *m_pUpgradeAction;
    QAction *m_pQuitAction;
};

TrayMenue.cpp

#include "TrayMenu.h"

TrayMenu::TrayMenu(QWidget *parent)
    : QMenu(parent)
{
    initActions();
}

TrayMenu::~TrayMenu()
{

}

// 添加菜单项
void TrayMenu::initActions()
{
    // 创建菜单项
    m_pShowAction = new QAction(QIcon(":/icon/open"), QString::fromLocal8Bit("显示"), this);
    m_pSettingAction = new QAction(QIcon(":/icon/setting"), QString::fromLocal8Bit("设置"), this);
    m_pHomePageAction = new QAction(QIcon(":/icon/home"), QString::fromLocal8Bit("登录网页"), this);
    m_pHelpAction = new QAction(QIcon(":/icon/help"), QString::fromLocal8Bit("帮助"), this);
    m_pAboutAction = new QAction(QIcon(":/icon/about"), QString::fromLocal8Bit("关于"), this);
    m_pUpgradeAction = new QAction(QIcon(":/icon/upgrade"), QString::fromLocal8Bit("检查更新"), this);
    m_pQuitAction = new QAction(QIcon(":/icon/quit"), QString::fromLocal8Bit("退出"), this);

    // 添加菜单项
    addAction(m_pShowAction);
    addAction(m_pSettingAction);
    addAction(m_pHomePageAction);
    addSeparator();
    addAction(m_pHelpAction);
    addAction(m_pAboutAction);
    addAction(m_pUpgradeAction);
    addSeparator();
    addAction(m_pQuitAction);

    // 连接信号-信号(仅列举连接显示窗口的信号)
    connect(m_pShowAction, SIGNAL(triggered(bool)), this, SIGNAL(showWindow()));
    connect(m_pQuitAction, SIGNAL(triggered(bool)), this, SIGNAL(QuitApp()));
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章