PDA

View Full Version : Contain mouse movement within drawing window



rdrnws
22nd June 2010, 08:35
Hello,

I have a simple widget and I draw simple shapes on it. I am selecting, say, a rectange, and moving it (dragging it actually) to another position in the drawing window. The problem is that, while dragging, I can move the mouse outside the drawing window, which is undesirable.

Is there a way to contain mouse movement within the drawing window? I guess some custom code needs to be placed in the mouseMoveEvent handler, but using what type of logic roughly?

TIA,

rdr

high_flyer
22nd June 2010, 09:11
You can use QCursor in a mouseMoveEvent(), and check the location of the mouse pointer, and move it back if it comes to the edge of your widget.
A better method probably would be to use QHoverEvent, where you can react to a HoverLeave event, to place the cursor back on the widget.

SixDegrees
22nd June 2010, 09:52
Note that by resetting the cursor to stay within an area, you are violating the user's expectations concerning mouse behavior. This is the sort of UI decision that frustrates users to no end, and I would very strongly recommend against it.

It would be better to stop moving the shapewhen the cursor leaves the bounding area; this is easily handled using mouse move events, and follows standard GUI behavior.