标签:
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>效果显示:
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);菜单效果:
void SystemTray::showMessage() { m_systemTray->showMessage("Information",//消息窗口标题 "There is a new message!",//消息内容 QSystemTrayIcon::MessageIcon::Information,//消息窗口图标 5000);//消息窗口显示时长 }消息效果:
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