Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a trayicon with Qt/c++ for all UNIX OS?

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:
      ;
  }
}
like image 925
SujaM Avatar asked Jan 24 '26 13:01

SujaM


1 Answers

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.

like image 123
Aurel Branzeanu Avatar answered Jan 27 '26 02:01

Aurel Branzeanu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!