System tray application Linux Qt/C++ - c++

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. Thanks much.
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);
}

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

I am commenting for the benefit of others here...
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. Also made sure that sni-qt is update 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.

Related

QTrayIcon doesn't show on Linux CentOS 7

I'm trying to add System Tray Icon to my QT application. Code works fine on Windows 7 to Windows 10 but the tray icon doesn't show up on CentOS 7. Application is working because it's getting also external signals and shows messages. Code bellow:
createTrayIcon();
trayIcon->show();
And function:
void Window::createTrayIcon()
{
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(logiAction);
trayIconMenu->addAction(managerAction);
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setIcon(QIcon(QString(":/resources/klatki/app.jpg")));
}

How to put an icon on file extensions of a Qt program on Windows Explorer

Here is the program. On the Web I found:
QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\.sp\\DefaultIcon",
QSettings::NativeFormat);
reg.setValue("Default", "C:\\Users\\Tomy\\Desktop\\package_directory"
"\\packages\\com.vendor.product\\data\\Spreadsheet.ico");
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
I put this in void MainWindow::writeSettings() and void MainWindow::readSettings() in the code. And then created an installer using Qt Installer Framework and installed it on Windows.
No icon! Any idea?
EDIT:
I this time removed the code above from both void MainWindow::readSettings() and void MainWindow::writeSettings() functions and added it to the MainWindow's constructor body, then re-did the rest and installed that new on my Windows.
Again, no changes in result.
There might be a problem either in the code or the script file. I'm using Qt 5.9.

Why are my qt file dialogs rendered incorrectly?

I'm currently creating a GUI with Qt 5.6 and I want to let the user open a file. I do this using the following code in my main window class:
#include <QFileDialog>
...
void MainWindow::loadScene()
{
QString loadPath = QFileDialog::getOpenFileName(this,
tr("Choose a scene file"), QString("."),
tr("Scene files (*.keyScene)"));
if(!loadPath.isEmpty())
resourceManager->loadScene(loadPath.toStdString());
}
I get the following dialog:
Screenshot of open file dialog
The system I use is Ubuntu 16.04, I've also tested on Ubuntu 14.04 and Mint 17.3. I get the exact same result on all of them.
When I compile the project in Windows everything works fine.
EDIT
It was suggested I play around with the DontUseNativeDialog flag when creating a file dialog, I can get a working Qt style file dialog with the following code:
QFileDialog dialog(this);
dialog.setAcceptMode(QFileDialog::AcceptOpen);
dialog.setOption(QFileDialog::Option::DontUseNativeDialog, true);
if(!dialog.exec())
return;
So the question is actually why native file dialogs aren't rendered correctly.

Qt system tray icon disappears when menu opened on OSX 10.10

I have simple qt code:
sticon = new QSystemTrayIcon(this);
QIcon icon(":/music.png");
sticon->setIcon(icon);
QMenu* stmenu = new QMenu(NULL);
action = new QAction("test",NULL);
stmenu->addAction(action);
sticon->setContextMenu(stmenu);
sticon->show();
connect(action, SIGNAL(triggered()), this, SLOT(clickText()));
And when I click on the menu, I get errors and Tray icon disappears:
QPainter::begin: Paint device returned engine == 0, type: 3
QPainter::setCompositionMode: Painter not active
QPainter::end: Painter not active, aborted
It works on OSX 10.10 + Qt 5.4
What is the problem?
This is Qt 5.4 bug:
On Mac OS X 10.9.5, run QSystemTrayIcon example included in the Qt
installation. Click on the icon shown on the menu bar to activate the
menu. Then click elsewhere to deactiviate it. The icon goes blank on
the menu bar. If you click on the blank space again, the icon
re-appears.
https://bugreports.qt-project.org/browse/QTBUG-42910?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

QSystemtrayicon: no image on Mac

I've got a problem with the QSystemTrayIcon class on Mac and Linux.
I made a program creating a System tray icon, and I have no problem on Windows, but under Linux (ubuntu 12) and Mac (OSX 10.8), the tray icon is here, but the image on it doesn't show up.
This is what I'm doing in the ctor of my class:
icon = new QIcon("trayIcon.png");
m_pTrayIcon = new QSystemTrayIcon();
m_pTrayIcon->setIcon(*icon);
m_pTrayContextMenu = new QMenu();
m_pTrayContextMenu->addAction(openSettings);
m_pTrayContextMenu->addAction(switchSyncMode);
m_pTrayContextMenu->addAction(openFolder);
m_pTrayContextMenu->addSeparator();
m_pTrayContextMenu->addAction(quit);
m_pTrayIcon->setContextMenu(m_pTrayContextMenu);
m_pTrayIcon->show();
I'm using the Qt 4.8 library.
Anyone have an idea?
Two possibilities
It cannot find the file. Try what happens if you give the global path.
It cannot load the png; see if loading QT module Multimedia helps.