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();
}
摘自:代碼人生