托盘一般有以下相关信息:
1. 托盘图标、托盘单击、托盘右键菜单
代码:
// 托盘图标
QSystemTrayIcon *mSystray = new QSystemTrayIcon(QIcon(":/img/icon.png"), this);
mSystray->setToolTip("提示信息"));
mSystray->setContextMenu(mMenu); // 上下文菜单 即右键菜单
mSystrayIcon->show(); // 显示托盘
// 托盘消息处理
connect(mSystray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this,SLOT(onActivated(QSystemTrayIcon::ActivationReason)));
void MainWindow::onActivated(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
case QSystemTrayIcon::Trigger: // 单击托盘处理
{
// ...
break;
}
case QSystemTrayIcon::DoubleClick: // 双击托盘处理
{
// ...
break;
}
default:
break;
}
}
菜单简单代码 创建了一个菜单,有2个菜单项
QAction action1 = new QAction(QIcon(":/img/new.png"), tr("new"), this);
QAction action2 = new QAction(QIcon(":/img/open.png"), tr("open"), this);
QMenu *mMenu = new QMenu(this);
mMenu ->addAction(action1);
mMenu ->addSeparator();
mMenu ->addAction(action2);