QT笔记- QSystemTrayIcon系统托盘功能完整示例
1. 创建托盘对象
// 创建托盘图标
QSystemTrayIcon * trayIcon = new QSystemTrayIcon(this);
QIcon icon("://icon/test.png");
trayIcon->setIcon(icon);
trayIcon->show();
trayIcon->connect(trayIcon, &QSystemTrayIcon::activated,
this, &MainWindow::_activated);
setWindowFlags(this->windowFlags() | Qt::Tool); // 设置窗口为Qt::Tool类型
2. _activated()槽函数
void MainWindow::_activated(QSystemTrayIcon::ActivationReason reason)
{
if(reason == QSystemTrayIcon::DoubleClick){
show();
} else if(reason == QSystemTrayIcon::Context){
QMenu menu;
QAction * closeAction = new QAction("Close", this);
// 连接关闭程序的信号槽,当点击该动作时,关闭整个应用程序
QObject::connect(closeAction, &QAction::triggered, qApp, &QApplication::quit);
menu.addAction(closeAction);
menu.exec(QCursor::pos()); // 在鼠标当前位置弹出菜单
}
}
原文地址:https://blog.csdn.net/qq_43058397/article/details/144449593
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!