5

I'm writing an application in C++ with Qt that utilizes the system tray.

I have implemented the system tray using a QSystemTrayIcon class as shown in the examples, but it doesn't have the same behavior as other system tray icons that are present on my computer.

For instance, I have Spotify installed on Ubuntu 12.04 and it shows a system tray icon with a drop down menu. With my application, it shows a system tray icon with a context menu, meaning you have to right click it to make the menu active.

With Spotify, all that needs to be done is to click on the icon and the menu will show.

What can I do to get native system tray icons in Ubuntu?

I'm fine with using specific code for X11/Linux and not the built-in Qt functions.

Here's my code:

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (trayIcon->isVisible()) {
        hide();
        event->ignore();
    }
}

void MainWindow::createActions()
{
    restoreAction = new QAction(tr("&Show"), this);
    connect(restoreAction, SIGNAL(triggered()), this, SLOT(show()));

    quitAction = new QAction(tr("&Exit"), this);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void MainWindow::createTrayIcon()
{
    trayIconMenu = new QMenu(this);
    accountsMenu = trayIconMenu->addMenu(tr("Accounts"));
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);
}
Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85

2 Answers2

5

Try to drop down menu from activated signal of QSystemTrayIcon.

void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
 {
     switch (reason) {
         case QSystemTrayIcon::Trigger:
             // show your menu here
     }
 }
graphite
  • 2,920
  • 22
  • 40
  • This will show the menu, but it only shows a context menu and I want to have it show a non-context menu. I think it might have something to do with Unity, but I'm not 100% sure. – Cameron Tinker May 18 '12 at 18:35
  • I will mark your answer as the correct answer, but I would still like to know how to tie into Ubuntu's global menu system. Qt 4.8.1 doesn't seem to use the global menu system by default and I can't find the setting to enable it in builds. – Cameron Tinker May 24 '12 at 23:06
  • What do you mean by 'global menu system'? Menu which usually at the top left cornrt of window which in ubuntu at the top of the screen? – graphite May 25 '12 at 08:07
  • I got global menu with QMenuBar in previous version of Qt. I'll check it with Qt 4.8.1. But I don't see how it is connected with tray. – graphite May 25 '12 at 08:16
  • I got global menu with QMenuBar in Qt 4.8.1. Do you want to show the same menu in menubar and when you click on tray icon? – graphite Jun 01 '12 at 07:11
  • Yes, I want to show the same styled menu when you click on the tray icon. I don't want to show the application menu when the tray icon is clicked, but the tray menu. I can try and provide screenshots if that would help explain what I'm trying to do. – Cameron Tinker Jun 02 '12 at 16:13
  • @CameronTinker Screenshots will be helpful. – graphite Jun 03 '12 at 10:33
  • I realize that I have not been phrasing this correctly since the initial question. What I want is a Unity indicator instead of a tray indicator. For some reason, I can't get Qt to automatically convert the tray indicator to a Unity indicator. Here's an example of the Unity indicator: http://files.iga-home.net/images/native.png and here's an example of the default tray indicator not converted: http://files.iga-home.net/images/non_native.png According to Qt's forum support, they say you need the package sni-qt to be installed. This is installed with Ubuntu 12.04 by default. – Cameron Tinker Jun 04 '12 at 04:06
1

I had the same issue when we deployed our product (built using Qt) on Ubuntu 12.04 LTS.

We use the qt.conf way of deployment. After a lot of hunting and going through the source on sni-qt I found that plugins need to be properly found out. So I created and copied the plugins from our build environment to the plugins directory relative to my application path mentioned in qt.conf against the 'Plugins = ' entry.

We also made sure that sni-qt is up-to-date and installed on the deployed Ubuntu 12.04 machine.

The menus appeared as they appear for other tray applications.

You can copy plugins from /usr/lib/i386-linux-gnu/qt4/plugins/ on a 32 bit machine or its equivalent path on 64 bit machine.

For this problem, plugin under systemtrayicon is the required one.

HTH.