qt - x11 限制鼠标移动

标签 qt ubuntu x11

我尝试将鼠标光标移动锁定在屏幕的左半部分。我有以下屏幕设置:

左侧是大小为 1120x1080 的 Qt 窗口,右侧是大小为 800x1080 的 GL 窗口。

我在 Ubuntu 12.10 下使用 Openbox 窗口管理器。窗口布局保持不变。

我需要限制鼠标移动到 Qt 窗口。

最佳答案

要让鼠标停留在窗口中,请使用以下命令启用鼠标移动:

setMouseTracking(true);

并覆盖void QWidget::mouseMovement( QMouseEvent * event )

void TheWindow::mouseMoveEvent ( QMouseEvent * event )
{
    // get window size without frame
    QRect s = geometry();

    // get current cursor position
    int x = event->globalX();
    int y = event->globalY();

    bool reset = false;

    // Check cursor position relative to window
    if (event->x() < 0) 
    {
        x -= event->x();
        reset = true;
    }
    else if (event->x() >= s.width())
    {
       x += s.width() - event->x() - 1;
       reset = true;
    }

    if (event->y() < 0)
    {
        y -= event->y();
        reset = true; 
    }
    else if (event->y() >= s.height())
    {
        y += s.height() - event->y() - 1;
        reset = true;
    }

    // if reset needed move cursor  
    if (reset) QCursor::setPos(x,y);

}

关于qt - x11 限制鼠标移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13398553/

相关文章:

c++ - 检测系统托盘\任务栏方向(X11)

regex - QSyntaxHighlighter 文档中的多行注释显示不正确

c++ - QImage精确读取像素数据

r - 在 Ubuntu 18.04 服务器上更新到 R 3.5.1 后出现 fatal error

php - Apache2返回php源码

linux - X11 XGrabPointer,如何清除释放鼠标之前的所有事件?

c++ - QT/SQLITE-数据库上的奇怪符号?

c++ - QTableView外部拖拽

ubuntu - Firebird 1.0、ubuntu、x86_64、odbc

java - 导出运行 JavaFX Scene3D 的 X Display