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. mapFromGlobal() and multiple monitors
QtWS25 Last Chance

mapFromGlobal() and multiple monitors

Scheduled Pinned Locked Moved Solved General and Desktop
mapfromglobalmultiple dpimultiple displamulti-screen
3 Posts 2 Posters 711 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.
  • B Offline
    B Offline
    bibasmall
    wrote on 28 Feb 2023, 10:07 last edited by bibasmall
    #1

    Hello!
    I need to detect if the cursor is on menu bar, so I use:

    //here I had to use nativeEvent, x and y seem to be correct
    long x = GET_X_LPARAM(msg->lParam);
    long y = GET_Y_LPARAM(msg->lParam);
    
    auto dpr = this->devicePixelRatioF();
    QPoint pos = ui.qMenuBarMain->mapFromGlobal(QPoint(x / dpr, y / dpr));
    if (ui.qMenuBarMain->rect().contains(pos))
        //some work
    

    It works fine if I use a single monitor, but with two with different DPI (dpr is 1.5 and 1 for the first and second respectively, if I use them separetely) something gets ruined, so I get negative relative coordinates in pos when using second monitor with less pixel density.
    I've checked if I could use:

    connect(this->window()->windowHandle(), &QWindow::screenChanged, [this](QScreen* screen) {
        auto dpr = screen->devicePixelRatio();
    });
    

    But dpr is same for both screens if they are used together. Though it doesn't help if I set dpr manually before division, so I think mapFromGlobal doesn't work as I expect.
    What am I doing wrong?

    S 1 Reply Last reply 28 Feb 2023, 19:51
    0
    • S SGaist
      28 Feb 2023, 19:51

      Hi,

      Which version of Qt ?
      On which OS ?

      B Offline
      B Offline
      bibasmall
      wrote on 1 Mar 2023, 15:46 last edited by
      #3

      @SGaist,
      Qt 6.3.2, Windows 10.

      I found that winapi coordinates I got eventually turned out to be different from QCursor::pos() when talking about switching between different scales. So I decided to use QCursor::pos() instead, and it helped:

      QPoint globalCursorPos = QCursor::pos();
      auto menuGeom = ui.qMenuBarMain->geometry().translated(ui.qMenuBarMain->mapToGlobal(QPoint(0, 0)));
      if (menuGeom.contains(globalCursorPos))
          //some work
      
      1 Reply Last reply
      0
      • B bibasmall
        28 Feb 2023, 10:07

        Hello!
        I need to detect if the cursor is on menu bar, so I use:

        //here I had to use nativeEvent, x and y seem to be correct
        long x = GET_X_LPARAM(msg->lParam);
        long y = GET_Y_LPARAM(msg->lParam);
        
        auto dpr = this->devicePixelRatioF();
        QPoint pos = ui.qMenuBarMain->mapFromGlobal(QPoint(x / dpr, y / dpr));
        if (ui.qMenuBarMain->rect().contains(pos))
            //some work
        

        It works fine if I use a single monitor, but with two with different DPI (dpr is 1.5 and 1 for the first and second respectively, if I use them separetely) something gets ruined, so I get negative relative coordinates in pos when using second monitor with less pixel density.
        I've checked if I could use:

        connect(this->window()->windowHandle(), &QWindow::screenChanged, [this](QScreen* screen) {
            auto dpr = screen->devicePixelRatio();
        });
        

        But dpr is same for both screens if they are used together. Though it doesn't help if I set dpr manually before division, so I think mapFromGlobal doesn't work as I expect.
        What am I doing wrong?

        S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 28 Feb 2023, 19:51 last edited by
        #2

        Hi,

        Which version of Qt ?
        On which OS ?

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

        B 1 Reply Last reply 1 Mar 2023, 15:46
        0
        • S SGaist
          28 Feb 2023, 19:51

          Hi,

          Which version of Qt ?
          On which OS ?

          B Offline
          B Offline
          bibasmall
          wrote on 1 Mar 2023, 15:46 last edited by
          #3

          @SGaist,
          Qt 6.3.2, Windows 10.

          I found that winapi coordinates I got eventually turned out to be different from QCursor::pos() when talking about switching between different scales. So I decided to use QCursor::pos() instead, and it helped:

          QPoint globalCursorPos = QCursor::pos();
          auto menuGeom = ui.qMenuBarMain->geometry().translated(ui.qMenuBarMain->mapToGlobal(QPoint(0, 0)));
          if (menuGeom.contains(globalCursorPos))
              //some work
          
          1 Reply Last reply
          0
          • B bibasmall has marked this topic as solved on 1 Mar 2023, 15:47

          1/3

          28 Feb 2023, 10:07

          • 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