I am writing a C ++ application with Qt that uses the system tray. I implemented the system tray using the QSystemTrayIcon class, as shown in the examples, but it does not have the same behavior as the other system tray icons that are present on my computer. For example, 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 an icon in the system tray with a context menu, that is, you must right-click it to make the menu active. With Spotify, all you have to do is click on the icon and display the menu. What can I do to get system tray icons in Ubuntu? I am fine using custom code for X11 / Linux, not Qt's built-in functions. Many thanks.
Here is 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);
}