1

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.

Ankur Shah
  • 125
  • 12
  • At what point do you get the geometry? if you want help you must provide a [mcve] – eyllanesc Nov 09 '18 at 18:51
  • 1
    It is not possible to get the geometry of the tray icon, as it is managed by the operating system. Qt only "tells" the system what to show and does not draw the icon itself – Felix Nov 10 '18 at 10:14
  • Got the same issue with PySide2, Python binding for Qt5, on macos. @Felix The example from the Udemy course I"m following works in the video and return the actual position of the Icon in the system tray, but not in my code. Beside what would be the point of a geometry() method implemented in QSystemTrayIcon class if it could not return the actual position of the icon and instead returned always coordinate 0,0 ? – Stephane Piriou Dec 03 '19 at 19:45

1 Answers1

1

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.

Stephane Piriou
  • 326
  • 2
  • 7