简体   繁体   English

Qt中奇怪的光标行为

[英]Weird cursor behavioir in Qt

In Qt (C++, MinGW, Windows 7), QCursor::setPos(...) seems to change the position of the cursor at the application level but not at the system level. 在Qt(C ++,MinGW,Windows 7)中, QCursor::setPos(...)似乎在应用程序级别而不是系统级别更改光标的位置。 For example, if you intercept the mouseMoveEvent and cancel out the movement of the cursor (with setMouseTracking(false) such that you do this cancellation only when a mouse button is pressed), then you'll see no cursor move as long as you maintain the mouse button pressed. 例如,如果您拦截mouseMoveEvent并取消了光标的移动(使用setMouseTracking(false)以便仅在按下鼠标按钮时执行此取消操作),则只要保持该状态,就不会看到任何光标移动按下鼠标按钮。 But then, when you release the button and move a little, the cursor will go to the place it would have been if you haven't blocked its movement. 但是,当您松开按钮并稍微移动时,光标将移至如果您没有阻止其移动的位置。 Is this behavior normal? 这是正常现象吗? How to get around it such that the mouse cursor is changed at the system level? 如何解决它,以便在系统级别更改鼠标光标?

If it is not clear enough, I can give code snippet. 如果不够清楚,我可以提供代码段。

Moving the cursor of a QWidget always uses the widget's coordinates. 移动QWidget的光标始终使用该控件的坐标。 For global mouse positioning, you should use the desktop widget (QDesktopWidget) instead. 对于全局鼠标定位,应改为使用桌面小部件(QDesktopWidget)。 You can get it through the QApplication::desktop() static function. 您可以通过QApplication::desktop()静态函数获取它。 Example: 例:

QApplication::desktop()->cursor().setPos(0,0);

This should move the mouse cursor to the top-left corner of the desktop. 这应将鼠标光标移动到桌面的左上角。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM