文章目录
  1. 1. 方法
  2. 2. 信号
  3. 3. 代码
  4. 4. 显示效果

QT中添加系统托盘使用 QSystemTrayIcon

方法

  1. void setContextMenu(QMenu *menu)

设置托盘的右键菜单

  1. void setIcon(const QIcon &icon)

设置托盘的图标

  1. void setToolTip(const QString &tip)

设置鼠标放上去的提示信息

  1. void showMessage(const QString &title,const QString &message, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information, int millisecondsTimeoutHint = 10000)

这个函数可以显示图示信息,参数分别为标题、信息内容、信息类型、显示时间
信息类型如下:
图标类型 | 值 | 说明
—————————- | —- | ——————–
QSystemTrayIcon::NoIcon | 0 | 没有图标显示.
QSystemTrayIcon::Information | 1 | 信息图标显示.
QSystemTrayIcon::Warning | 2 | 警告图标显示.
QSystemTrayIcon::Critical | 3 | 出错图标显示.

信号

  1. void activated(QSystemTrayIcon::ActivationReason reason)

用户激活图标时发出,参数为激活方式,详情如下:

激活方式 说明
SystemTrayIcon::Unknown 0 未知原因
QSystemTrayIcon::Context 1 请求系统托盘条目的上下文菜单。
QSystemTrayIcon::DoubleClick 2 双击系统托盘
QSystemTrayIcon::Trigger 3 点击系统托盘
QSystemTrayIcon::MiddleClick 4 鼠标中键点击系统托盘
  1. void messageClicked()

当用户点击消息提示时发出

代码

头文件代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSystemTrayIcon>

QT_BEGIN_HEADER
class QAction;
QT_END_HEADER

class MainWindow : public QMainWindow
{
Q_OBJECT

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

protected slots:
void widgetScreenshot(); // 界面截图
// 托盘图标点击处理槽函数
void trayIconActivated(QSystemTrayIcon::ActivationReason reason);

private:
void initWidget(); // 初始化界面

private:
QSystemTrayIcon* m_pTrayIcon;
QAction* m_pActOpen;
QAction* m_pActClose;
QAction* m_pActScreen;
};

#endif // MAINWINDOW_H

cpp文件代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "mainwindow.h"
#include <QStandardPaths>
#include <QGuiApplication>
#include <QScreen>
#include <QAction>
#include <QMenu>
#include <QDateTime>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
initWidget();
}

MainWindow::~MainWindow()
{

}

// 初始化界面
void MainWindow::initWidget()
{
m_pTrayIcon = new QSystemTrayIcon(this);
m_pTrayIcon->setIcon(QIcon(":/Icon/Resources/qt-logo.ico")); //设置图标
m_pTrayIcon->setToolTip(tr("System Tray Icon")); //设置鼠标放上去显示的信息

//右键菜单
QMenu *pMenu = new QMenu(this);

// 打开界面
m_pActOpen = new QAction(tr("Open widget"), this);
connect(m_pActOpen, SIGNAL(triggered()), this, SLOT(show()));
pMenu->addAction(m_pActOpen);

// 关闭界面
m_pActClose = new QAction(tr("Close widget"), this);
connect(m_pActClose, SIGNAL(triggered()), this, SLOT(hide()));
pMenu->addAction(m_pActClose);

// 界面截图
m_pActScreen = new QAction(tr("Widget screenshot"), this);
connect(m_pActScreen, SIGNAL(triggered()), this, SLOT(widgetScreenshot()));
pMenu->addAction(m_pActScreen);

m_pTrayIcon->setContextMenu(pMenu); //设置右键菜单
connect(m_pTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
m_pTrayIcon->show(); //显示
}

// 界面截图
void MainWindow::widgetScreenshot()
{
// 获取屏幕类
QScreen *pScreen = QGuiApplication::primaryScreen();
// 获取第一个屏幕的图片
QPixmap mPixmap = pScreen->grabWindow(0);
// 获取系统图片目录
QString savePath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
// 设置名称为当前时间
savePath = QString( "%1/%2.png").arg(savePath).arg(QDateTime::currentDateTime().toString("yyyyMMddHHmmss"));
// 保存图片
qDebug() << "savePath" << savePath;
mPixmap.save(savePath);
}

void MainWindow::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
case QSystemTrayIcon::Trigger: // 鼠标点击
setWindowState(Qt::WindowActive);
activateWindow();
break;
default:
break;
}
}

显示效果

系统托盘

文章目录
  1. 1. 方法
  2. 2. 信号
  3. 3. 代码
  4. 4. 显示效果