QT 托盘菜单代码

#include "widget.h"
#include <QApplication>
#include<QSystemTrayIcon>//包含托盘对象
#include<QIcon> //包含图标
int main(int argc,char *argv[])
QApplication a(argc,argv);
Widget w;//窗口对象
QSystemTrayIcon sysTray(QIcon("://img/icon.png"),&m);//加载图片
QMenu menu;//菜单对象
auto showAct=new QAction("show",&sysTray);//新建一个菜单用于打开
auto exitAct=new QAction("exit",&sysTray);//新建一个菜单按钮用于退出
//2个信号连接 Qobject::connect(showAct,
&QAction::triggered,[&](){ w.setVisible(true); }); Qobject::connect(exitAct,&QAction::triggered,[&](){ QApplication::quit(); });
//把对象和加入到菜单中。 menu.addAction(showAct); menu.addAction(exitAct); sysTray.setContextMenu(
&menu); sysTray.show(); w.show(); return a.exec();

 

posted @ 2024-09-13 09:06  MaopaiJD  阅读(29)  评论(0)    收藏  举报