[英]System tray application with Qt 5.9.1 for macOS
在“更高級的視圖”上,我無能為力,但是我最近實現了一個托盤圖標應用程序。
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);
}
請注意,為了在單擊窗口中的“關閉”圖標時不關閉整個應用程序,必須覆蓋close事件:
void Launcher::closeEvent(QCloseEvent *event) {
hide();
event->ignore();
}
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.