我有一个 Qt 应用程序,我想在系统托盘中显示它。我想要的行为是,如果用户单击应用程序的关闭按钮,那么该应用程序将隐藏在系统托盘中但不会退出。
我的代码main.cpp
是:
if (QSystemTrayIcon::isSystemTrayAvailable())
{
QObject *root = engine.rootObjects().at(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
QAction *showAction = new QAction(QObject::tr("Show"), window);
window->connect(showAction, SIGNAL(triggered()), window, SLOT(show()));
QAction *hideAction = new QAction(QObject::tr("Hide"), window);
window->connect(hideAction, SIGNAL(triggered()), window, SLOT(hide()));
QAction *quitAction = new QAction(QObject::tr("&Quit"), window);
window->connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
QObject::connect(qApp,SIGNAL(aboutToQuit()),window,SLOT(hide()));
QMenu *trayIconMenu = new QMenu();
trayIconMenu->addAction(showAction);
trayIconMenu->addAction(hideAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
QSystemTrayIcon *trayIcon = new QSystemTrayIcon(window);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setToolTip("xxx");
trayIcon->setIcon(QIcon("xxx.png"));
trayIcon->show();
}
现在我无法连接aboutToQuit
信号并将应用程序隐藏在托盘中,即
QObject::connect(qApp,SIGNAL(aboutToQuit()),window,SLOT(hide()));
线路不正确但我没有收到任何错误等。除此之外一切正常。有人可以告诉我我做错了什么以及如何我可以实现我想要的行为吗?我还想知道我是否有正确的信号来连接,或者我是否应该尝试连接到其他信号。提前致谢。