Qt 5,获取鼠标在屏幕上的位置 [英] Qt 5, get the mouse position in a screen
问题描述
首先,我想提到的是,我发现了相关的帖子如何在Qt中获得鼠标在屏幕上的位置?,但对我来说这根本不起作用。我做了一些测试,结果却没有按预期工作,所以我决定写一篇新文章谈论我所做的测试并找到替代解决方案。
这就是我用来进行测试的代码:
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());
//在第一个屏幕上的位置。
QPoint pos0 = QCursor :: pos(screen0);
//在第二个屏幕上的位置。
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());
//获取不带屏幕的位置。
QPoint pos = QCursor :: pos();
printf( pos:%d,%d \n,pos.x(),pos.y());
我期望的是,只有一个屏幕会返回有效位置,因为光标仅在一个屏幕上,而不是两个屏幕上。但是事实并非如此,两个位置( pos0
和 pos1
)的值完全相同,如我们所见在输出上:
screen0 DVI-D-0
screen1 HDMI-0
pos 0:1904 ,1178
pos 1:1904,1178
pos:1904,1178
由于两个位置的值相同,所以我不知道光标在哪个屏幕上。我不知道这是正常现象还是错误,因为文档没有说明屏幕参数不是鼠标所在的屏幕时会发生什么。
<我的想法是将应用程序(由必须检测选定屏幕的Qt守护程序执行)打开/启动到鼠标所在的屏幕。我知道使用 libX11 是可能的,因为我过去曾经这样做过,但是我需要使用Qt 5,而且我不知道如何使用Qt检测选定的屏幕。 / p>
我还使用 QApplication
和 QDesktopWidget
进行了其他测试没有运气的类。
那真的很奇怪。解决方法是,您可以尝试以下操作:
QPoint globalCursorPos = QCursor :: pos();
int mouseScreen = qApp-> desktop()-> screenNumber(globalCursorPos);
现在您知道了光标所在的屏幕。然后您可以找到该屏幕中光标的位置
QRect mouseScreenGeometry = qApp-> desktop()-> screen(mouseScreen)-> geometry();
QPoint localCursorPos = globalCursorPos-mouseScreenGeometry.topLeft();
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.
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 (pos0
and pos1
) has the exactly same value, as we can see on the output:
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.
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 libX11 it'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.
I also made other tests, using QApplication
and QDesktopWidget
classes with no luck.
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();
这篇关于Qt 5,获取鼠标在屏幕上的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!