0

I would like to build a pure system tray based application for macOS like you know it from Dropbox or 1Password mini.

Currently I have C++ code with a QGuiApplication, the main things are to hide the dock icon and to provide a more advanced view than just a menu when clicking on the system tray icon.

This question already was answered here but I’d prefer a solution with C++ or even QML.
Is this possible in Qt? How would I do it?

1 Answers1

0

On the "more advanced view" I can't help, however I recently implemented a tray icon application.

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);
}

Note that in order not to close the complete application when you click the "close" icon in the window, you have to overwrite the close event:

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

Hope this helps.

Ph03n1x
  • 794
  • 6
  • 12
  • Did the answer help? – Ph03n1x Jul 31 '17 at 15:57
  • No, but thanks anyway! I've chosen a QML solution by using the QML type "SystemTrayIcon" and toggle the visibility of my mainWindow when the user clicks the system tray icon. However i still have problems to set the right position of my window because i don't know where the system tray icon will exactly be placed. – julian_wiese Aug 03 '17 at 06:56