Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Determine tray icon locataion on screen.

Determine tray icon locataion on screen.

Scheduled Pinned Locked Moved Unsolved General and Desktop
qscreenqsystemtrayicon
3 Posts 3 Posters 603 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dariusz
    wrote on 3 Sept 2021, 09:32 last edited by Dariusz 9 Mar 2021, 09:37
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Sept 2021, 20:49 last edited by
      #2

      Hi,

      I would go check the contextual menu handling in QSystemTrayIcon's sources, there might something there to help you.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • C Online
        C Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on 3 Sept 2021, 23:14 last edited by
        #3

        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 and corner 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.

        1 Reply Last reply
        4

        1/3

        3 Sept 2021, 09:32

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved