不用ClipCursor(),直接上跨平台代码:
void MyMainWindow::leaveEvent(QEvent * event)
{
// Get the window geometry & cursor position
const QRect & rect = geometry();
QPoint position = QCursor::pos();
// Check the bounds
qint32 x = qBound(rect.left(), position.x(), rect.right());
qint32 y = qBound(rect.top(), position.y(), rect.bottom());
// Adjust the cursor
if (x != position.x() || y != position.y())
QCursor::setPos(x, y);
event->accept();
QMainWindow::leaveEvent(event);
}
这个博客介绍了一种在Qt环境中实现跨平台的光标限制方法。在`leaveEvent`事件中,通过获取窗口几何信息和光标位置,检查并调整光标使其保持在窗口边界内。这使得用户在窗口外移动鼠标时,光标会被自动限制在窗口范围内。
1213

被折叠的 条评论
为什么被折叠?



