Determine tray icon locataion on screen.
-
Hey
So this is a hippi wippi one. I want to have a popup widget when user click on tray. But for that I need to know where I should position the widget...
Now the icon can be in 4 different "areas" on screen, depending on taskbar settings, top/left/right/bottom. On top of that the icon can be on any number of screens... so if there is 5 screen config... tray can be anywhere or on all of them...I was looking over screens 1st:
auto app = qobject_cast<QGuiApplication *>(QApplication::instance()); QScreen *s = nullptr; auto tg = mTrayIcon->geometry(); for (auto &a: app->screens()) { if (a->geometry().contains(tg)) { s = a; break; } }
To determine which screen I'm on.
Now this does not sadly tell me if its left of primary/right/etc etc as far as I can tell...
So finding out the position of tray is a little hard...As once I find it, I have to place the widget properly next to it/above it/below it/ etc...
Now so far I was trying to determine that using widget.geo().x()/y() and then finding its on a screen.
But screen x/y coordinates are in... "global" space, not per sreen... so screen to right would have screen0.width()+screen1.width()==screen2.x()
And not 0...Anyway, u'm fairly sure I lost you already... maybe to "simplify it" how can I find out in which corner given tray icon is on screen?
TIA
-
Hi,
I would go check the contextual menu handling in QSystemTrayIcon's sources, there might something there to help you.
-
You can do something like this:
QPoint tray_center = mTrayIcon->geometry().center(); QRect screen_rect = qApp->screenAt(tray_center)->geometry(); QPoint screen_center = screen_rect.center(); Qt::Corner corner = Qt::TopLeftCorner; if (tray_center.x() > screen_center.x() && tray_center.y() <= screen_center.y()) corner = Qt::TopRightCorner; else if (tray_center.x() > screen_center.x() && tray_center.y() > screen_center.y()) corner = Qt::BottomRightCorner; else if (tray_center.x() <= screen_center.x() && tray_center.y() > screen_center.y()) corner = Qt::BottomLeftCorner;
So yeah,
screenAt
will give you the screen you're on andcorner
will tell you which corner of the screen are you in.Then you can use
qApp->screenAt(tray_center)->availableGeometry()
to determine the workable area of that screen and use the corner information to place your widget. You don't need to know if it's a primary , left or right screen. The available geometry is enough for your purpose.