适用于 macOS 的 Qt 5.9.1 的系统托盘应用程序
System tray application with Qt 5.9.1 for macOS
我想为macOS构建一个纯粹的基于系统托盘的应用程序,就像您从Dropbox或1Password mini中知道的那样。
目前,我已使用QGui应用程序C++代码,主要是隐藏停靠图标,并在单击系统托盘图标时提供比菜单更高级的视图。
这个问题已经在这里得到了回答,但我更喜欢使用 C++甚至QML的解决方案。
这在Qt中可能吗?我该怎么做?
在"更高级的视图"上,我无能为力,但是我最近实现了一个托盘图标应用程序。
void Launcher::InitializeTrayIcon() {
CreateTrayActions();
CreateTrayIcon();
}
void Launcher::CreateTrayActions() {
restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
openConfig = new QAction(tr("&Open Configuration"), this);
connect(openConfig, SIGNAL(triggered()), this, SLOT(openConfigFile()));
languageSelection = new QMenu(tr("Select Language"), this);
QIcon englishFlag(":/Resources/FlagEnglish.png");
activateEnglishLanguage = new QAction(englishFlag, tr("English"), this);
connect(activateEnglishLanguage, SIGNAL(triggered()), this,
SLOT(changeLanguageToEnglish()));
QIcon germanFlag(":/Resources/FlagGerman.png");
activateGermanLanguage = new QAction(germanFlag, tr("German"), this);
connect(activateGermanLanguage, SIGNAL(triggered()), this,
SLOT(changeLanguageToGerman()));
}
void Launcher::CreateTrayIcon() {
// Create Menu for Tray Icon
trayIconMenu = new QMenu(this);
languageSelection = trayIconMenu->addMenu(tr("Select Language"));
languageSelection->addAction(activateEnglishLanguage);
languageSelection->addAction(activateGermanLanguage);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addAction(openConfig);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
// Create tray icon by passing its Menu
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
// Add functionality on double click
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,
SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
SetTrayIconLogo();
trayIcon->show();
}
void Launcher::SetTrayIconLogo() {
QIcon icon(":/Resources/icon.ico");
trayIcon->setIcon(icon);
}
请注意,为了在单击窗口中的"关闭"图标时不关闭整个应用程序,您必须覆盖关闭事件:
void Launcher::closeEvent(QCloseEvent *event) {
hide();
event->ignore();
}
希望这有帮助。
相关文章:
- 如果整个应用程序是虚拟映射的,为什么 new 会进行系统调用?
- 在不使用系统的情况下从C++应用程序格式化 Linux 中的 SD 卡
- 如何为模块化应用程序实现C++插件系统
- 设置我的应用程序 API 感知并防止系统使其模糊和错误定位
- 如何设置Qt应用程序以使用类Unix系统的实际环境变量?
- 通知 Windows 系统应用程序字体已被卸载/删除
- 即使在我为我的应用程序授予管理员后也无法从 Windows 系统复制目录
- 如何将控制台应用程序放入系统托盘通知C++?
- 适用于 macOS 的 Qt 5.9.1 的系统托盘应用程序
- 错误:尝试在全新系统安装中构建应用程序后未定义对'...'的引用
- 打开应用程序主窗口 在系统托盘图标中的其他应用程序之上
- 强制系统 QT 库使用应用程序附带的 openssl 库
- 如何监视应用程序进行的Windows系统API调用?
- 如何在应用程序正在运行的系统上查看所需的Windows API函数
- 如何在Qt中打开外部应用程序并将其最小化到系统托盘
- 将WM_CHANGEUISTATE发送到窗口应用程序后,当用鼠标访问菜单时,助记符不会显示在系统菜单上
- QT应用程序作为Android系统应用程序
- 在windows上用c++获取系统应用程序的文件大小
- 如何使一个c++程序与鼠标点击和X窗口系统应用程序交互
- 如何从用户应用程序发送自定义消息到系统应用程序