一、效果:
程序关闭后,程序并没有退出,而是放入了托盘中;点击恢复原始大小,或者双击托盘图标,可以恢复程序原来的窗口。如下图。
那qt是如何实现这样的办法呢,其实就是用到了 QSystemTrayIcon类,通常有两种方法去写,第一种比较简单,直接利用QSystemTrayIcon ,来达到一些基本的托盘程序,也是本文章要讲的。第二种是继承QSystemTrayIcon类,这样子类设计可以使得托盘程序更加功能强大,接下来给大家讲解简单的托盘。
二、主要代码
重写了closeEvent()函数,我们点击关闭按钮时候执行了里面的closeEvent()函数hide(),这样窗口就不会关闭了。托盘菜单的退出才是真的调用quit()关闭整个程序。直接在窗口类中增加以下代码:
.h头文件增加:
/
//托盘相关开始,添加成员及成员函数
/
private:
QMenu *trayMenu;//托盘菜单
QSystemTrayIcon *tray;//托盘图标添加成员
void InitTray();
protected:
void closeEvent(QCloseEvent *event);// 重写申明
void hideEvent(QHideEvent *event);// 重写申明
private slots:
void iconActivated(QSystemTrayIcon::ActivationReason ireason);
/
//托盘相关结束
/
.cpp实现文件增加
/
//托盘相关开始,添加成员及成员函数
/
//构造函数初始化
void DlgStudent::InitTray()
{
//***托盘***
tray= new QSystemTrayIcon(this);//初始化托盘对象tray
//tray->setIcon(QIcon(QPixmap(":/images/logo.ico")));//设定托盘图标,引号内是自定义的png图片路径
//tray->setIcon(QIcon(":/images/logo.ico"));//设定托盘图标,引号内是自定义的png图片路径
tray->setIcon(qApp->windowIcon());//设定托盘图标,引号内是自定义的png图片路径
tray->setToolTip(qApp->applicationName());//托盘时鼠标悬停提示文字
QString title="上机提示";
QString text="请填写并提交上机信息";
tray->show();//让托盘图标显示在系统托盘上
tray->showMessage(title,text,QSystemTrayIcon::Information,3000); //最后一个参数为提示时长,默认10000,即10s
//创建菜单项动作
QAction* minimizeAction = new QAction("窗口最小化", this);
connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
QAction* maximizeAction = new QAction("窗口最大化", this);
connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
QAction* restoreAction = new QAction("恢复原始大小", this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
QAction* quitAction = new QAction("退出", this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); //关闭应用
//创建托盘菜单
trayMenu = new QMenu(this);
trayMenu->addAction(minimizeAction);
trayMenu->addAction(maximizeAction);
trayMenu->addAction(restoreAction);
trayMenu->addSeparator();
trayMenu->addAction(quitAction);
tray->setContextMenu(trayMenu);
connect(tray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
//添加槽函数
void DlgStudent::iconActivated(QSystemTrayIcon::ActivationReason ireason)
{
switch (ireason)
{
case QSystemTrayIcon::Trigger:
//this->showNormal();
//this->showFullScreen();
break;
case QSystemTrayIcon::DoubleClick:
//this->showNormal();
this->showFullScreen();
break;
case QSystemTrayIcon::MiddleClick:
break;
default:
break;
}
}
//拦截桌面窗口事件
void DlgStudent::closeEvent(QCloseEvent *event)
{
#ifdef _DEF_TRAY_
if(tray->isVisible())
{
hide(); //隐藏窗口
tray->showMessage("托盘", "this is your first to get it");
event->ignore(); //忽略事件
}
#endif
}
void DlgStudent::hideEvent(QHideEvent *event)
{
#ifdef _DEF_TRAY_
if(tray->isVisible())
{
hide(); //隐藏窗口
event->ignore(); //忽略事件
}
#endif
}
/
//托盘相关结束
/
三、完整代码
DlgStudent.h
#ifndef DLGSTUDENT_H
#define DLGSTUDENT_H
/
//托盘相关开始,添加头文件
/
#include <QSystemTrayIcon> //t托盘类
#include <QDesktopServices> //桌面事件类
#include <QCloseEvent>
/
//托盘相关结束
/
#include <QMainWindow>
#include "chook.h"
namespace Ui {
class DlgStudent;
}
class DlgStudent : public QMainWindow
{
Q_OBJECT
public:
explicit DlgStudent(QWidget *parent = 0);
~DlgStudent();
public slots:
void Slot_DlgStudent_Show();//学生页面显示
void Slot_DlgStudent_Hide();//学生页面隐藏
public:
int InitDlg();//初始化窗口
int InitMask();//初始化屏蔽
public:
CHook m_hook;
private slots:
void on_pushButton_quit_clicked();
public slots:
void Slot_loginSuccess();//登录成功,窗体显示
private:
Ui::DlgStudent *ui;
/
//托盘相关开始,添加成员及成员函数
/
private:
QMenu *trayMenu;//托盘菜单
QSystemTrayIcon *tray;//托盘图标添加成员
void InitTray();
protected:
void closeEvent(QCloseEvent *event);// 重写申明
void hideEvent(QHideEvent *event);// 重写申明
private slots:
void iconActivated(QSystemTrayIcon::ActivationReason ireason);
/
//托盘相关结束
/
void on_pushButton_submit_studentInfo_clicked();
};
#endif // DLGSTUDENT_H
DlgStudent.cpp
#pragma execution_character_set("utf-8")//解决中文乱码
#include "DlgStudent.h"
#include "QMsgBox.h"
#include "externparameters.h"
#include "ui_DlgStudent.h"
#include <QProcess>
#include <signalctrl.h>
//#define _DEF_TRAY_ //定义托盘
DlgStudent::DlgStudent(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::DlgStudent)
{
ui->setupUi(this);
#ifdef _DEF_TRAY_
InitTray();//初始化最小化到托盘
#endif
InitDlg();
InitMask();
//以下为测试代码
//#define PROCESS_NAME "sscom5.13.1.exe"
//QString str_app_name = PROCESS_NAME;
//QByteArray chStrArray = str_app_name.toLocal8Bit();
//const char* pName = chStrArray.constData();//当前进程名
//QProcess::startDetached(str_app_name);
}
DlgStudent::~DlgStudent()
{
delete ui;
}
//学生页面显示
void DlgStudent::Slot_DlgStudent_Show()
{
this->show();
}
//学生页面隐藏
void DlgStudent::Slot_DlgStudent_Hide()
{
this->hide();
}
//初始化窗体
int DlgStudent::InitDlg()
{
ui->menuBar->hide();
ui->mainToolBar->hide();
ui->statusBar->hide();
//Qt::Window: 无论是否有父窗口部件,新窗口部件都是一个窗口,通常有一个窗口边框和一个标题栏
//Qt::Tool新窗口部件是一个工具(tool)窗口,它通常是一个用于显示工具按钮的小窗口。如果一个工具窗口有父窗口部件,则它将显示在父窗口的部件上面,否则相当于使用了Qt::WindowStaysOnTopHint提示。
//Qt::FramelessWindowHint产生一个无窗口边框的窗口,此时用户无法移动该窗口和改变它的大小
//Qt::WindowStaysOnTopHint告知窗口系统,该窗口应该停留在所有其他窗口的上面
//this->setWindowFlags (Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);//没有标题栏,就是隐藏窗口标题栏、最大化、最小化、关闭按钮,总在最上面的窗口
//this->setWindowFlags(Qt::Tool | Qt::WindowTitleHint | Qt::WindowStaysOnTopHint);//保留标题,关闭按钮无效,隐藏其他,总在最上面的窗口
this->setWindowFlags(Qt::WindowTitleHint | Qt::WindowStaysOnTopHint);//保留标题,关闭按钮无效,隐藏其他,总在最上面的窗口
//this->setWindowIcon(QIcon(":/images/user.ico"));//设置任务栏窗口图标
return 1;
}
//初始化屏蔽
int DlgStudent::InitMask()
{
//m_hook.StartHook();
return 1;
}
void DlgStudent::on_pushButton_quit_clicked()
{
//m_hook.StopHook();
this->hide();
//QApplication* app;
//app->quit();
}
//登录成功,窗体显示
void DlgStudent::Slot_loginSuccess()
{
this->showFullScreen();
}
/
//托盘相关开始,添加成员及成员函数
/
//构造函数初始化
void DlgStudent::InitTray()
{
//***托盘***
tray= new QSystemTrayIcon(this);//初始化托盘对象tray
//tray->setIcon(QIcon(QPixmap(":/images/logo.ico")));//设定托盘图标,引号内是自定义的png图片路径
//tray->setIcon(QIcon(":/images/logo.ico"));//设定托盘图标,引号内是自定义的png图片路径
tray->setIcon(qApp->windowIcon());//设定托盘图标,引号内是自定义的png图片路径
tray->setToolTip(qApp->applicationName());//托盘时鼠标悬停提示文字
QString title="上机提示";
QString text="请填写并提交上机信息";
tray->show();//让托盘图标显示在系统托盘上
tray->showMessage(title,text,QSystemTrayIcon::Information,3000); //最后一个参数为提示时长,默认10000,即10s
//创建菜单项动作
QAction* minimizeAction = new QAction("窗口最小化", this);
connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
QAction* maximizeAction = new QAction("窗口最大化", this);
connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
QAction* restoreAction = new QAction("恢复原始大小", this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
QAction* quitAction = new QAction("退出", this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); //关闭应用
//创建托盘菜单
trayMenu = new QMenu(this);
trayMenu->addAction(minimizeAction);
trayMenu->addAction(maximizeAction);
trayMenu->addAction(restoreAction);
trayMenu->addSeparator();
trayMenu->addAction(quitAction);
tray->setContextMenu(trayMenu);
connect(tray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
//添加槽函数
void DlgStudent::iconActivated(QSystemTrayIcon::ActivationReason ireason)
{
switch (ireason)
{
case QSystemTrayIcon::Trigger:
//this->showNormal();
//this->showFullScreen();
break;
case QSystemTrayIcon::DoubleClick:
//this->showNormal();
this->showFullScreen();
break;
case QSystemTrayIcon::MiddleClick:
break;
default:
break;
}
}
//拦截桌面窗口事件
void DlgStudent::closeEvent(QCloseEvent *event)
{
#ifdef _DEF_TRAY_
if(tray->isVisible())
{
hide(); //隐藏窗口
tray->showMessage("托盘", "this is your first to get it");
event->ignore(); //忽略事件
}
#endif
}
void DlgStudent::hideEvent(QHideEvent *event)
{
#ifdef _DEF_TRAY_
if(tray->isVisible())
{
hide(); //隐藏窗口
event->ignore(); //忽略事件
}
#endif
}
/
//托盘相关结束
/
//学生机填写的信息提交
void DlgStudent::on_pushButton_submit_studentInfo_clicked()
{
StudentInfo studentInfo;
studentInfo.name = ui->lineEdit_name->text();
studentInfo.grades = ui->lineEdit_grades->text();
studentInfo.classes = ui->lineEdit_classes->text();
if(studentInfo.name.isEmpty()
||studentInfo.grades.isEmpty()
||studentInfo.classes.isEmpty())
{
QMsgBox::warning("警告", "信息不可为空!", QMessageBox::Ok);
return;
}
SignalCtrl::getInstance()->Slot_ServerCtrl_StudentInfo(studentInfo);
}