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.
  • bibasmallB Offline
    bibasmallB Offline
    bibasmall
    wrote on 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?

    SGaistS 1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      Which version of Qt ?
      On which OS ?

      bibasmallB Offline
      bibasmallB Offline
      bibasmall
      wrote on 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
      • bibasmallB bibasmall

        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?

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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

        bibasmallB 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Which version of Qt ?
          On which OS ?

          bibasmallB Offline
          bibasmallB Offline
          bibasmall
          wrote on 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
          • bibasmallB bibasmall has marked this topic as solved on

          • Login

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