main.cpp
QApplication app(argc, argv);
//判断系统是否支持 系统托盘功能
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
QMessageBox::critical(0, QObject::tr("Systray"),
QObject::tr("I couldn't detect any system tray "
"on this system."));
return 1;
}
//关闭 隐式退出在最后一个窗口关闭时(这个示例该行语句注释也没区别)
QApplication::setQuitOnLastWindowClosed(false);
windows.h
protected:
//重载关闭事件
void closeEvent(QCloseEvent *event) override;
private slots:
//点击托盘图标时的处理
void iconActivated(QSystemTrayIcon::ActivationReason reason);
//显示托盘弹出消息
void showMessage();
//点击托盘消息后的处理
void messageClicked();
private:
//托盘图标右键 菜单动作
QAction *minimizeAction;
QAction *maximizeAction;
QAction *restoreAction;
QAction *quitAction;
//系统托盘
QSystemTrayIcon *trayIcon;
//托盘菜单
QMenu *trayIconMenu;
void Window::createActions()
{
minimizeAction = new QAction(tr("Mi&nimize"), this);
connect(minimizeAction, &QAction::triggered, this, &QWidget::hide);
maximizeAction = new QAction(tr("Ma&ximize"), this);
connect(maximizeAction, &QAction::triggered, this, &QWidget::showMaximized);
restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal);
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
}
void Window::createTrayIcon()
{
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(maximizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
}
//是否显示托盘图标
connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, &QSystemTrayIcon::setVisible);
//托盘消息点击事件
connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &Window::messageClicked);
//托盘图标点击事件
connect(trayIcon, &QSystemTrayIcon::activated, this, &Window::iconActivated);
void Window::closeEvent(QCloseEvent *event)
{
//如果显示了托盘,关闭事件忽略,窗体隐藏
if (trayIcon->isVisible()) {
QMessageBox::information(this, tr("Systray"),
tr("The program will keep running in the "
"system tray. To terminate the program, "
"choose <b>Quit</b> in the context menu "
"of the system tray entry."));
hide();
event->ignore();
}
}
//托盘图标点击消息处理
void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick: //改变图标
iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) % iconComboBox->count());
break;
case QSystemTrayIcon::MiddleClick:
showMessage();
break;
default:
;
}
}
//显示消息
void Window::showMessage()
{
showIconCheckBox->setChecked(true);
QSystemTrayIcon::MessageIcon msgIcon = QSystemTrayIcon::MessageIcon(
typeComboBox->itemData(typeComboBox->currentIndex()).toInt());
if (msgIcon == QSystemTrayIcon::NoIcon) {
QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex()));
trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon,
durationSpinBox->value() * 1000);
} else {
trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon,
durationSpinBox->value() * 1000);
}
}