先看一下效果:
代码如下
#ifndef MAINWINDOW_H标签:Demo,系统托盘,myTrayIcon,QAction,QSystemTrayIcon,void,menu,MainWindow From: https://blog.51cto.com/u_15854865/5811253
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QCloseEvent>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QSystemTrayIcon *m_myTrayIcon;
QMenu *m_menu;
QAction *m_miniSizeAction;
QAction *m_maxSizeAction;
QAction *m_restoreWinAction;
QAction *m_quitAction;
private:
void initTrayMenu();
void initTrayIcon();
private slots:
void iconActivated(QSystemTrayIcon::ActivationReason reason);
protected:
void closeEvent(QCloseEvent *event);
};
#endif // MAINWINDOW_H
源文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initTrayMenu();
initTrayIcon();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initTrayMenu()
{
//这里&后面的字母是自己定义的
m_miniSizeAction = new QAction("Min(&V)",this);
m_maxSizeAction = new QAction("Max(&X)",this);
m_restoreWinAction = new QAction("Reduction(&R)",this);
m_quitAction = new QAction("Quit(&Q)",this);
m_miniSizeAction->setIcon(QIcon(":/ooopic_1489563147.ico"));
m_maxSizeAction->setIcon(QIcon(":/ooopic_1489563147.ico"));
m_quitAction->setIcon(QIcon(":/ooopic_1489563147.ico"));
m_menu = new QMenu((QWidget*)QApplication::desktop());
m_menu->addAction(m_miniSizeAction);
m_menu->addAction(m_maxSizeAction);
m_menu->addAction(m_restoreWinAction);
m_menu->addSeparator(); //加入一个分离符
m_menu->addAction(m_quitAction);
connect(m_miniSizeAction, SIGNAL(triggered()), this, SLOT(hide()));
connect(m_maxSizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
connect(m_restoreWinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
connect(m_quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}
void MainWindow::initTrayIcon()
{
//判断系统是否支持系统托盘图标
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
return;
}
m_myTrayIcon = new QSystemTrayIcon(this);
QIcon tmpIcon = QIcon(":/ooopic_1489563147.ico");
m_myTrayIcon->setIcon(tmpIcon); //设置图标图片
setWindowIcon(tmpIcon); //把图片设置到窗口上
m_myTrayIcon->setToolTip("SystemTrayIcon V1.0"); //托盘时,鼠标放上去的提示信息
m_myTrayIcon->showMessage("SystemTrayIcon","Hi,This is my trayIcon",QSystemTrayIcon::Information,10000);
m_myTrayIcon->setContextMenu(m_menu); //设置托盘上下文菜单
m_myTrayIcon->show();
connect(m_myTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch(reason) {
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
showNormal();
break;
case QSystemTrayIcon::MiddleClick:
m_myTrayIcon->showMessage("SystemTrayIcon","Hi,This is my trayIcon",QSystemTrayIcon::Information,10000);
break;
default:
break;
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (m_myTrayIcon->isVisible()) {//最小化
m_myTrayIcon->showMessage("SystemTrayIcon","Hi,This is my trayIcon",QSystemTrayIcon::Information,5000);
hide();
event->ignore();
} else {
event->accept();
}
}
————————————————