Qt 实现系统托盘,托盘菜单,托盘消息
系统托盘就是运行的某个程序在电脑右下角的小图标,相信大家都很熟悉。QT 提供了QSystemTrayIcon类用于实现程序的托盘。QT提供了托盘示例,在examples文件夹下面,大家可以自行研究。
下面是我自己进行实现的一个示例,主要实现的功能:
1.托盘显示,托盘图标添加;
2.托盘激活事件分发;
3.托盘菜单;
4.托盘消息,点击消息,显示主窗口。
想要显示 托盘图标,首先需要创建一个QSystemTrayIcon对象。可以给此对象设置一些信息,比如ToolTip,比如图标。需要调用show,托盘才能显示。
m_systemTray = new QSystemTrayIcon(this); m_systemTray->setIcon(QIcon(":/SystemTray/Resources/ico.png")); m_systemTray->setToolTip("SystemTray Program"); m_systemTray->show();
给程序添加图片资源,需要编辑.qrc文件,将图片所在的路径添加进来就可以:
<RCC> <qresource prefix="SystemTray"> <file>Resources/ico.png</file> </qresource> </RCC>
效果显示:
托盘激活事件,包括点击,双击,右键等。QSystemTrayIcon提供了信号给我们使用,信号的参数是激活类型,我们连接自己的槽函数就可以对类型进行分支,从而执行对应的操作:
Constant Value Description QSystemTrayIcon::Unknown 0 Unknown reason QSystemTrayIcon::Context 1 The context menu for the system tray entry was requested QSystemTrayIcon::DoubleClick 2 The system tray entry was double clicked QSystemTrayIcon::Trigger 3 The system tray entry was clicked QSystemTrayIcon::MiddleClick 4 The system tray entry was clicked with the middle mouse button
连接我们自己的槽函数:
connect(m_systemTray, &QSystemTrayIcon::activated, this, &SystemTray::activeTray);//点击托盘,执行相应的动作
在槽函数中,分别调用了不同的函数,进行不同的操作:
void SystemTray::activeTray(QSystemTrayIcon::ActivationReason reason) { switch (reason) { case QSystemTrayIcon::Context: showMenu(); break; case QSystemTrayIcon::DoubleClick: showWindow(); break; case QSystemTrayIcon::Trigger: showMessage(); break; } }
要实现菜单,我们需要创建一个QMenu并创建自己需要的QAction,添加到QMenu里面,将QMenu设置给QSystemTrayIcon既可。我们可以给每个QAction连接不同的槽函数,执行不同的操作。
m_menu = new QMenu(this); m_action1 = new QAction(m_menu); m_action2 = new QAction(m_menu); m_action1->setText("Show Window"); m_action2->setText("Show Message"); m_menu->addAction(m_action1); m_menu->addAction(m_action2); connect(m_action1, &QAction::triggered, this, &SystemTray::showWindow); connect(m_action2, &QAction::triggered, this, &SystemTray::showMessage); m_systemTray->setContextMenu(m_menu);
菜单效果:
消息显示也很简单,QSystemTrayIcon提供了showMessage函数来显示消息窗口,此函数有四个参数,分别如下:
void SystemTray::showMessage() { m_systemTray->showMessage("Information",//消息窗口标题 "There is a new message!",//消息内容 QSystemTrayIcon::MessageIcon::Information,//消息窗口图标 5000);//消息窗口显示时长 }
消息效果:
QSystemTrayIcon提供了messageClicked信号,点击消息,可以触发我们自己连接的槽函数,执行我们想要实现的动作:
connect(m_systemTray, &QSystemTrayIcon::messageClicked, this, &SystemTray::showWindow);//点击消息框,显示主窗口
主窗口显示:
程序一开始主界面是隐藏的,双击托盘可以显示主窗口。
完整代码:
#ifndef SYSTEMTRAY_H #define SYSTEMTRAY_H #include <QtWidgets/QWidget> #include <QSystemTrayIcon> #include <QMenu> class SystemTray : public QWidget { Q_OBJECT public: SystemTray(QWidget *parent = 0); ~SystemTray(); protected: void activeTray(QSystemTrayIcon::ActivationReason reason); void showWindow();//显示窗体 void showMessage();//消息框 void showMenu();//显示菜单 private: QSystemTrayIcon *m_systemTray; QMenu *m_menu; QAction *m_action1; QAction *m_action2; }; #endif // SYSTEMTRAY_H
#include "systemtray.h" SystemTray::SystemTray(QWidget *parent) : QWidget(parent) { m_systemTray = new QSystemTrayIcon(this); m_menu = new QMenu(this); m_action1 = new QAction(m_menu); m_action2 = new QAction(m_menu); m_action1->setText("Show Window"); m_action2->setText("Show Message"); m_menu->addAction(m_action1); m_menu->addAction(m_action2); connect(m_action1, &QAction::triggered, this, &SystemTray::showWindow); connect(m_action2, &QAction::triggered, this, &SystemTray::showMessage); m_systemTray->setContextMenu(m_menu); m_systemTray->setIcon(QIcon(":/SystemTray/Resources/ico.png")); m_systemTray->setToolTip("SystemTray Program"); m_systemTray->show(); connect(m_systemTray, &QSystemTrayIcon::activated, this, &SystemTray::activeTray);//点击托盘,执行相应的动作 connect(m_systemTray, &QSystemTrayIcon::messageClicked, this, &SystemTray::showWindow);//点击消息框,显示主窗口 } SystemTray::~SystemTray() { } void SystemTray::activeTray(QSystemTrayIcon::ActivationReason reason) { switch (reason) { case QSystemTrayIcon::Context: showMenu(); break; case QSystemTrayIcon::DoubleClick: showWindow(); break; case QSystemTrayIcon::Trigger: showMessage(); break; } } void SystemTray::showMenu() { m_menu->show(); } void SystemTray::showWindow() { this->show(); } void SystemTray::showMessage() { m_systemTray->showMessage("Information",//消息窗口标题 "There is a new message!",//消息内容 QSystemTrayIcon::MessageIcon::Information,//消息窗口图标 5000);//消息窗口显示时长 }
#include "systemtray.h" #include <QtWidgets/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); SystemTray w; //w.show(); w.hide();//初始化时,主窗口隐藏,便于观察效果 return a.exec(); }
<RCC> <qresource prefix="SystemTray"> <file>Resources/ico.png</file> </qresource> </RCC>
交流 QQ:1245178753
本文地址:http://blog.csdn.net/u011417605/article/details/51322997
源码下载:http://download.csdn.net/detail/u011417605/9510891