1 添加两个成员变量
QSystemTrayIcon *trayicon;
QMenu *trayiconMenu;
2 添加一个槽函数用于响应在托盘图标及托盘菜单上的鼠标点击操作
void onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason reason);
3 在构造函数中对添加的两个变量进行初始化
trayicon = new QSystemTrayIcon(this);
//创建QIcon对象,参数是图标资源,值为项目的资源文件中图标的地址
QIcon icon("F:/Windows QT/TrayIcon/bottom.png");
trayiconMenu = new QMenu(this);
//为托盘菜单添加菜单项
trayiconMenu->addAction(ui->action_quit);
//为托盘菜单添加分隔符
trayiconMenu->addSeparator();
//将创建的QIcon对象作为系统托盘图标
trayicon->setIcon(icon);
//显示托盘图标
trayicon->show();
//设置系统托盘提示
trayicon->setToolTip(tr("托盘测试"));
//将创建菜单作为系统托盘菜单
trayicon->setContextMenu(trayiconMenu);
//在系统托盘显示气泡消息提示
trayicon->showMessage(tr("hahaya"), tr("托盘测试"), QSystemTrayIcon::Information, 5000);
//为系统托盘绑定单击信号的槽 即图标激活时
connect(trayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason)));
4 实现槽函数onSystemTrayIconClicked
void MainWindow::onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
//单击
case QSystemTrayIcon::Trigger:
//双击
case QSystemTrayIcon::DoubleClick:
//恢复窗口显示
this->setWindowState(Qt::WindowActive);
this->show();
break;
default:
break;
}
}
5 当点击窗口的关闭按钮时,将程序托盘,则重写类的closeEvent函数
void MainWindow::closeEvent(QCloseEvent *event)
{
if(trayicon->isVisible())
{
hide();
event->ignore();
}
}
6 点击“退出”菜单的槽函数,点击退出时托盘
void MainWindow::on_action_quit_triggered()
{
this->hide();
this->close();
}
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QCloseEvent>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void on_action_quit_triggered();
void onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason reason);
protected:
void closeEvent(QCloseEvent *event);
private:
QSystemTrayIcon *trayicon;
QMenu *trayiconMenu;
};
#endif // MAINWINDOW_H
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QCloseEvent>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void on_action_quit_triggered();
void onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason reason);
protected:
void closeEvent(QCloseEvent *event);
private:
QSystemTrayIcon *trayicon;
QMenu *trayiconMenu;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include <QtGui>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
trayicon = new QSystemTrayIcon(this);
//创建QIcon对象,参数是图标资源,值为项目的资源文件中图标的地址
QIcon icon("F:/Windows QT/TrayIcon/bottom.png");
trayiconMenu = new QMenu(this);
//为托盘菜单添加菜单项
trayiconMenu->addAction(ui->action_quit);
//为托盘菜单添加分隔符
trayiconMenu->addSeparator();
//将创建的QIcon对象作为系统托盘图标
trayicon->setIcon(icon);
//显示托盘图标
trayicon->show();
//设置系统托盘提示
trayicon->setToolTip(tr("托盘测试"));
//将创建菜单作为系统托盘菜单
trayicon->setContextMenu(trayiconMenu);
//在系统托盘显示气泡消息提示
trayicon->showMessage(tr("hahaya"), tr("托盘测试"), QSystemTrayIcon::Information, 5000);
//为系统托盘绑定单击信号的槽 即图标激活时
connect(trayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
//单击
case QSystemTrayIcon::Trigger:
//双击
case QSystemTrayIcon::DoubleClick:
//恢复窗口显示
this->setWindowState(Qt::WindowActive);
this->show();
break;
default:
break;
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if(trayicon->isVisible())
{
hide();
event->ignore();
}
}
void MainWindow::on_action_quit_triggered()
{
this->hide();
this->close();
}
//mainwindow.cpp
#include <QtGui>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
trayicon = new QSystemTrayIcon(this);
//创建QIcon对象,参数是图标资源,值为项目的资源文件中图标的地址
QIcon icon("F:/Windows QT/TrayIcon/bottom.png");
trayiconMenu = new QMenu(this);
//为托盘菜单添加菜单项
trayiconMenu->addAction(ui->action_quit);
//为托盘菜单添加分隔符
trayiconMenu->addSeparator();
//将创建的QIcon对象作为系统托盘图标
trayicon->setIcon(icon);
//显示托盘图标
trayicon->show();
//设置系统托盘提示
trayicon->setToolTip(tr("托盘测试"));
//将创建菜单作为系统托盘菜单
trayicon->setContextMenu(trayiconMenu);
//在系统托盘显示气泡消息提示
trayicon->showMessage(tr("hahaya"), tr("托盘测试"), QSystemTrayIcon::Information, 5000);
//为系统托盘绑定单击信号的槽 即图标激活时
connect(trayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
//单击
case QSystemTrayIcon::Trigger:
//双击
case QSystemTrayIcon::DoubleClick:
//恢复窗口显示
this->setWindowState(Qt::WindowActive);
this->show();
break;
default:
break;
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if(trayicon->isVisible())
{
hide();
event->ignore();
}
}
void MainWindow::on_action_quit_triggered()
{
this->hide();
this->close();
}
//main.cpp
#include <QtGui/QApplication>
#include <QTextCodec>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
//main.cpp
#include <QtGui/QApplication>
#include <QTextCodec>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}