簡體   English   中英

適用於macOS的Qt 5.9.1的系統托盤應用程序

[英]System tray application with Qt 5.9.1 for macOS

我想為macOS構建基於純系統任務欄的應用程序 ,就像您從Dropbox或1Password mini所知道的那樣。

當前,我有一個帶有QGuiApplication的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);
}

請注意,為了在單擊窗口中的“關閉”圖標時不關閉整個應用程序,必須覆蓋close事件:

void Launcher::closeEvent(QCloseEvent *event) {
  hide();
  event->ignore();
}

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM