X11 mouse restriction

I am trying to block the mouse cursor in the left half of the screen. I have the following screen settings:

On the left side is a Qt window of size 1120x1080, on the right is a GL window of size 800x1080.

I am using Openbox window manager in Ubuntu 12.10. The window layout remains fixed.

I need to limit mouse movement in the Qt window.

+4
source share
2 answers

To make the mouse stay in the window, enable mouse movement with:

setMouseTracking(true); 

and override 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); } 
0
source

featuring QGraphicsItem::itemChange() . If you have an item that you want to limit to a specific area, override itemChange() for that item and monitor the QGraphicsItem::ItemPositionHasChanged changes to see if you want items to be placed outside your area of ​​interest and prevent this by returning a position from within that area . eg:

 QVariant QGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value) { switch (change) { case ItemPositionHasChanged: if(x() < -200 || y() < -200 || x() > 200 || y() > 200) setPos(0, 0); graph->itemMoved(); break; default: break; }; return QGraphicsItem::itemChange(change, value); } 
0
source

Source: https://habr.com/ru/post/1446236/


All Articles