做软件都会有一个功能最小化到系统托盘,虽然网上的资料有很关于系统托盘的,但是我还是写下,下面是一些功能代码:
在头文件中声明一个QSystemTrayIcon 变量
protected slots:
void m_sHandleShowWindowAction();
void m_sHandleExitAction();
void m_sHandleSysTrayIcon(QSystemTrayIcon::ActivationReason);
private:
QSystemTrayIcon *m_sTrayIcon;
//托盘的右键菜单
QMenu *m_mTrayMenu;
QAction *m_aShowWindow;
QAction *m_aExit;
在构造函数中将其new出来
m_sTrayIcon = new QSystemTrayIcon(this);
m_sTrayIcon->setIcon(QIcon("你的Ico目录"));
m_sTrayIcon->setToolTip(tr("for example");
m_mTrayMenu = new QMenu(this);
m_aShowWindow = new QAction(tr("显示窗口"),this);
m_aExit = new QAction(tr("退出"),this);
m_mTrayMenu->addAction(m_aShowWindow);
m_mTrayMenu->addAction(m_aExit);
m_sTrayIcon->setContextMenu(m_mTrayMenu); //添加右键菜单
QObject::connect(m_sTrayIcon,SIGNAL(QSystemTrayIcon::ActivationReason),this,SLOT(m_sHandleSysTrayIcon(QSystemTrayIcon::ActivationReason)));
QObject::connect(m_aShowWindow,SIGNAL(triggered()),this,SLOT(m_sHandleShowWindowAction()));
QObject::connect(m_aExit,SIGNAL(triggered()),this,SLOT(m_sHandleExitAction()));
当你点击主界面的关闭按钮,弹出一个对话框,有两个选择一个是退出到托盘,另一个是退出程序,
当你选择退出到托盘,点击确定按钮
你在确定按钮的槽函数里面
this->hide();//隐藏主界面
m_sTrayIcon->show();//显示最小化托盘
然后点击右键菜单的Action,你在其槽函数中写相应的功能。