PDA

View Full Version : Lock Mouse Image Position



squeegedog
17th February 2014, 03:26
Hi All!

I am changing my mouse cursor to an image that the user can place onto different spots of my scene. I have the place-able locations laid out into a grid of squares, so what I would like to do is restrict the movement of the mouse cursor image to the exact grid square that it would be placed within if clicked at any moment.

I'm almost positive this will involve creating an override of the QCursor class, but I can't really figure out how to get started based upon the documentation.

From my readings, I don't believe that changing the hotspot would hold the image still as I think it just changes what part of the image is received as the "click location".

Perhaps temporarily drop the mouse cursor onto the scene, make the mouse have no cursor, and either restore the image to the mouse when it leaves a grid location, or restore the typical pointer upon a click?

Any advice would be helpful, thanks.

squeegedog
19th February 2014, 08:18
Well if anyone is ever interested in accomplishing this task, I managed to get it working using the method proposed above.

Create a private graphicsobject in the scene header and add it to the scene (this isnt absolutely necessary, but the other option would involve creating and destroying the object many times)

In the graphicsscene mousemoveevent:
Set the mouse cursor to the desired image
When the mouse is within the bounds of a "drop" location, set the graphicsobject to the mouse cursor image and the location to the top-left pixel of the "drop" location and set the mouse cursor to no cursor
When the mouse leaves a "drop" location, restore the image from the graphicsobject, hide it, and set it to the mouse cursor image

In the graphicsscene mousepressevent:
Check again to ensure mouse is within "drop" location
Add a new object to the scene containing the mouse image
Regardless of passing the check, restore mouse to original pointer, hide the graphicsobject (setting the pixmap to NULL generates errors on some systems, hide works across all platforms)