QT-托盘QSystemTrayIcon示例使用


前言

有时候软件关闭,我们从系统栏也是能正常操作后台软件的,那么这个时候我们使用的其实托盘效果而已。


一、演示效果

请添加图片描述
请添加图片描述

二、关键程序

代码如下:

#include "QtWidgetsApplication1.h"
#include <QSystemTrayIcon>
#include <QMenu>
#include <QIcon>
QtWidgetsApplication1::QtWidgetsApplication1(QWidget *parent)
    : QDialog(parent)
{
    ui.setupUi(this);

	QMenu *m = new QMenu(this);
	QIcon icon(u8":/Resouce/托盘.png");
	m_pSystemTray = new QSystemTrayIcon(this);
	m_pSystemTray->setIcon(icon);
	m_pSystemTray->setToolTip(u8"托盘测试");
	m_pMinimumAct = new QAction(u8"最小化", this);
	connect(m_pMinimumAct, SIGNAL(triggered()), this, SLOT(hide()));
	m_pMaximumAct = new QAction(u8"最大化", this);
	connect(m_pMaximumAct, SIGNAL(triggered()), this, SLOT(showMaximized()));
	m_pRestoreAct = new QAction(u8"恢复窗口", this);
	connect(m_pRestoreAct, SIGNAL(triggered()), this, SLOT(showNormal()));
	m_pQuitAct = new QAction(u8"退出", this);
	connect(m_pQuitAct, SIGNAL(triggered()), qApp, SLOT(quit()));

	connect(m_pSystemTray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivatedSysTrayIcon(QSystemTrayIcon::ActivationReason)));

	QMenu *pContextMenu = new QMenu(this);
	pContextMenu->addAction(m_pMinimumAct);
	pContextMenu->addAction(m_pMaximumAct);
	pContextMenu->addAction(m_pRestoreAct);
	pContextMenu->addSeparator();
	pContextMenu->addAction(m_pQuitAct);

	m_pSystemTray->setContextMenu(pContextMenu);
	m_pSystemTray->show();
	close();
}


void QtWidgetsApplication1::closeEvent(QCloseEvent *event)
{
	if (m_pSystemTray->isVisible())
	{
		hide();
		m_pSystemTray->showMessage(u8"托盘", u8"该程序托盘正在运行!");
	}
}


void QtWidgetsApplication1::slotActivatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
	switch (reason)
	{
	case QSystemTrayIcon::Trigger:
		//单击托盘图标
		break;

	case QSystemTrayIcon::DoubleClick:
		//双击托盘图标
		//双击后显示主程序窗口
		this->show();
		break;

	default:
		break;
	}
}

三、下载链接

https://download.csdn.net/download/u013083044/87576323

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的大海贼

联系博主,为您提供有价值的资源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值