2
votes

I'm working with Qt/C++ creating my own app, I want it to be cross-platform, so far everything it's been working great with Qt, the only problem its with the tray-icon.

I create a tray-icon and add a menu to it, this works fine in windows but it's not working in Linux. I test my app in two computers and got two different respond.

In my desktop (Ubuntu 14.04) the tray-Icon appears on the right side of the task-bar as expected but it doesn't show me the menu. And in the laptop (also Ubuntu 14.04) the tray-icon appears on the left side of the task-bar but in this case it does shows me the menu and a message when I double click the icon.

I don't know if there's another way to do this but here it's my code.

if (QSystemTrayIcon::isSystemTrayAvailable())
{
  //trayicon
  trayIcon = new QSystemTrayIcon(this);
  trayIcon->setIcon(QIcon(":/Imagenes/iconosPERFQ-23.png"));
  trayIcon->setToolTip(tr("PerfQ Client"));
  trayIcon->setVisible(true);

  connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
                 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
  //Menu
  logoutAction = new QAction(tr("&Logout"), this);
  trayIconMenu = new QMenu(this);
  trayIconMenu->addAction(logoutAction);
  // Add the menu to the trayicon
  trayIcon->setContextMenu(trayIconMenu);

  connect (logoutAction,SIGNAL(triggered()), this, SLOT(logout()));
}

and the slot for the activated signal on the trayicon

void Task::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
  switch (reason)
  {
    case QSystemTrayIcon::DoubleClick:
      QMessageBox::information(this,"Double Click", "Double click has been press on trayicon");
      break;
    default:
      ;
  }
}
2

2 Answers

2
votes

The code's not the fault - Ubuntu changed the system notification few years ago from sni (status notifier indicator) to appnotifier which is working via D-Bus and is not backwards compatible with sni.

Though, there is a package in Ubuntu - sni-qt and sni-qt:386 - which helps to show systray icons for applications built with Qt prior to 5.xx version.

Also, if you're building with Qt 5.xx - upgrade Qt to 5.5. It is working with the new appindicator via D-Bus.

0
votes

There is no QSystemTrayIcon::activated on Ubuntu. The only thing that happens when you (single) click the icon is that the context menu opens.

This is a fact you have to deal with, a UI choice borrowed from OS X. It dramatically simplifies the interaction with tray icons, because there is no single/double/right click which every application uses differently. Just the menu that does everything.

Regarding your different behavior: could you check those Ubuntu versions again? It looks like the differences came with the transition from Gnome to Unity. The Gnome way of dealing with tray icons differs in many points from what a current Ubuntu 15.04 is doing in Unity.