mapFromGlobal() and multiple monitors
-
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? -
@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 useQCursor::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
-
@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 useQCursor::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
-