C++ Qt 5,获取鼠标在屏幕中的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32040202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:04:40  来源:igfitidea点击:

Qt 5, get the mouse position in a screen

c++qtqt5multiscreen

提问by armitage

First of all, I'd like to mention that I found that related post How to get the mouse position on the screen in Qt?but it "just didn't work" for me. I made some tests, and the results didn't work as I expected, so I decided to make a new post to talk about the test I made and to find an alternative solution.

首先,我想提一下,我发现了相关的帖子如何在 Qt 中获取屏幕上的鼠标位置?但它对我来说“不起作用”。我做了一些测试,结果并没有像我预期的那样工作,所以我决定发一个新帖子来谈谈我所做的测试并寻找替代解决方案。

That's the code I used to make the test:

这是我用来进行测试的代码:

QScreen *screen0 = QApplication::screens().at(0);
QScreen *screen1 = QApplication::screens().at(1);

printf("screen0 %s \n", screen0->name().toStdString().c_str());
printf("screen1 %s \n", screen1->name().toStdString().c_str());

// Position on first screen.
QPoint pos0 = QCursor::pos(screen0);

// Position on second screen.
QPoint pos1 = QCursor::pos(screen1);

printf("pos 0: %d, %d \n", pos0.x(), pos0.y());
printf("pos 1: %d, %d \n", pos1.x(), pos1.y());

// Get position without screen.
QPoint pos = QCursor::pos();
printf("pos: %d, %d \n", pos.x(), pos.y());

What I was expecting, is that only one screen would return a valid position, since the cursor is only at one screen, not on both. But it's not the case, the both positions (pos0and pos1) has the exactly same value, as we can see on the output:

我所期望的是,只有一个屏幕会返回一个有效位置,因为光标只在一个屏幕上,而不是在两个屏幕上。但事实并非如此,这两个位置 (pos0pos1) 具有完全相同的值,正如我们在输出中所看到的:

screen0 DVI-D-0 
screen1 HDMI-0 
pos 0: 1904, 1178 
pos 1: 1904, 1178 
pos: 1904, 1178 

Since the both positions has the same values, I can't know at which screen is the cursor. I don't know if that's a normal behavior or a bug, since the documentation doesn't say what happens when the screen argument isn't the screen where the mouse is.

由于两个位置具有相同的值,我不知道光标在哪个屏幕上。我不知道这是正常行为还是错误,因为文档没有说明当 screen 参数不是鼠标所在的屏幕时会发生什么。

My idea, is to open/launch an application (executed by a Qt daemon that must detect the selected screen) to the screen where the mouse is. I know that with libX11it's possible, because I did it in the past, but I need to work with Qt 5, and I can't figure out how to do detect the selected screen with Qt.

我的想法是在鼠标所在的屏幕上打开/启动一个应用程序(由必须检测所选屏幕的 Qt 守护进程执行)。我知道使用libX11是可能的,因为我过去做过,但是我需要使用 Qt 5,而且我不知道如何使用 Qt 检测选定的屏幕。

I also made other tests, using QApplicationand QDesktopWidgetclasses with no luck.

我还做了其他测试,使用QApplicationQDesktopWidget类没有运气。

回答by Rafael Monteiro

That's really weird. As a workaround, you could try this:

这真的很奇怪。作为解决方法,您可以尝试以下操作:

QPoint globalCursorPos = QCursor::pos();
int mouseScreen = qApp->desktop()->screenNumber(globalCursorPos);

Now you know which screen the cursor is in. Then you could find the cursor position within that screen doing this:

现在你知道光标在哪个屏幕上。然后你可以在那个屏幕中找到光标位置,这样做:

QRect mouseScreenGeometry = qApp->desktop()->screen(mouseScreen)->geometry();
QPoint localCursorPos = globalCursorPos - mouseScreenGeometry.topLeft();

回答by GPMueller

This may seem like a trivial solution, but on my KDE it works (I ran into the same problems originally). If you want to determine the local mouse coordinates with respect to a widget (this will be in device pixels and relative to the top left corner of the widget I believe) you can use

这似乎是一个微不足道的解决方案,但在我的 KDE 上它有效(我最初遇到了同样的问题)。如果您想确定相对于小部件的本地鼠标坐标(这将以设备像素为单位,并且我相信相对于小部件的左上角),您可以使用

QWidget::mapFromGlobal(QCursor::pos());

i.e. call this->mapFromGlobal.

即调用this->mapFromGlobal

回答by Lekensteyn

To figure out on which screen you are, you can iterate throught QGuiApplication::screens()and check whether the cursor fits in the geometryof the screen.

要确定您在哪个屏幕上,您可以遍历QGuiApplication::screens()并检查光标是否适合屏幕的几何形状

Here is a more complex example to compute the native cursor position (note the additional work needed to work with High DPI screens):

下面是一个更复杂的例子来计算原生光标位置(注意使用高 DPI 屏幕所需的额外工作):

QPoint getNativeCursorPosition()
{
    QPoint pos = cursorPosToNative(QCursor::pos());

    // Cursor positions from Qt are calculated in a strange way, the offset to
    // the origin of the current screen is in device-independent pixels while
    // the origin itself is native!

    for (QScreen *screen : QGuiApplication::screens()) {
        QRect screenRect = screen->geometry();
        if (screenRect.contains(pos)) {
            QPoint origin = screenRect.topLeft();
            return origin + (pos - origin) * screen->devicePixelRatio();
        }
    }

    // should not happen, but try to find a good fallback.
    return pos * qApp->devicePixelRatio();
}

回答by samarth

This may work for you? It did for me

这可能对你有用吗?它对我有用

QDesktopWidget *widget = QApplication::desktop(); QPosition globalCursorPosition = widget->cursor().pos();

QDesktopWidget *widget = QApplication::desktop(); QPosition globalCursorPosition = widget->cursor().pos();

回答by armitage

Sice it seems that it can't be done with Qt (at least with my system configuration, and it seems that also in Windows) I decided to use the libX11 to make that implementation, which works like charm.

Sice 似乎不能用 Qt 来完成(至少在我的系统配置中,而且似乎在 Windows 中也是如此)我决定使用 libX11 来实现该实现,它的工作原理很迷人。

It's not an ideal solution because I wanted to only use Qt, but it works.

这不是一个理想的解决方案,因为我只想使用 Qt,但它有效。