I'm using Ubuntu (17.04) with the Unity desktop. I'm unable to get any geometry information for QSystemTrayIcon:
trayIconMenu = new QMenu(this);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
QPixmap trayImage(":/icon.png");
QIcon icon(trayImage);
trayIcon->setIcon(icon);
setWindowIcon(icon);
trayIcon->show();
QRect rect = trayIcon->geometry();
qDebug() << "Tray GEO: " << rect;
This prints out the following:
Tray GEO: QRect(0,0 0x0)
Everything is zeroed out which can't be correct.
I had the same issue in PySide2, a python bidding for Qt5. Here what I discovered.
The geometry of the QSystemTrayIcon
object (a QRect
object) is not known in your app until the signal activated
of the QSystemTrayIcon
object is emitted at least once.
Once you click the actual icon of your app in the system tray, the activated
signal is emitted and then the geometry()
method can return the initialized QRect
object with the actual values of its position and size.
In short, you have to activate the system tray icon first in order for it to send the initialized QRect
with its actual position and size values.
To get the position of the system tray icon when the app launch, which rely on screen resolution and OS, you have to manually emit the activated
signal in your code.
If the goals is to show a window near the system tray icon, you can either hide the window at launch, and it will show up near the system tray once you click on the system tray Icon by positioning in your code your window to the same coordinates as the system tray icon; or you emit the activated
signal manually upon launch if you want to show the window directly near the system tray icon.