PDA

View Full Version : QGraphicsItem / QGraphicsProxyWidget: how to snap to grid



RThaden
14th February 2010, 13:48
Hi all,

I have a QGraphicsScene with some QGraphicsProxyWidgets that the user can move around with the mouse.
I'd like to snap the widgets to an (invisible) grid.
I tried to reimplement mouseReleaseEvent in a way that the event->pos() is set to a multiple of 5 but that doesn't work properly.
Any hint where to look for a solution?

Rainer

Palmik
14th February 2010, 19:52
Well... I believe that there are multiple solutions I will mention two of them, probably the best one is to use QGraphicsGridLayout and calculate position (which I believe is what you tried to do, do not know what might go wrong), or create "dummy" items of the same size also placed in grid and use them to catch the drops and move the actual items accordingly.

RThaden
15th February 2010, 10:20
Thanks Palmik for your suggestions

I had a look at QGraphicsGridLayout and started implementing it.
However, there are still some issues that I need to think about.

The idea is to have an area where several small widgets can be arranged "freely". Maybe, there will be a photo in the background and the widgets (which represent devices in the real world) are arranged according to their position on the photo.
What I want to achieve is, that two widgets are not placed on top of eachother and that they are aligned to a grid of, say, 5 pixel to make it look neat.
Therefore, my idea was simply to align the positions to multiples of 5 pixel.
It must also be possible to have one widget placed at scene coordinates, e.g. (10px,10px) and the next one at (15px, 60px) when the dimensions of the widget are e.g. (80px, 40px).

Maybe, I could use a QGridlayout with a preferred column and row width of 5 px and span my widgets over several rows and columns?

Rainer

Palmik
15th February 2010, 21:07
I misunderstood you first post then, I'm sorry. I'm also not sure if you can achieve this (if I got you right, you want something like for example grid in gimp where the items can be sticky to the grid) with QGraphicsGridLayout, but I might be wrong.
But it should be possible to do it with grabbing the pos() of the dropEvent, then round it to the closest 5px and then set the item position to the rounded one (which is - as I understand - what you already tried but with mouseReleaseEvent). If it still does not work for you, post the code.