PDA

View Full Version : drag-drop question



zgulser
1st July 2009, 12:53
Hi,

I want to ask how can I make a graphic item drag and drop through on a certain line?

Thanks in advance

wysota
1st July 2009, 13:19
What do you mean by "through on a certain line"?

zgulser
1st July 2009, 14:07
Hi,

I wanted to mean along a certain line. In other words, whereever I drag my item in the view, I want it to move along a certain line according to the dragging movements.

wysota
1st July 2009, 14:49
In that case you probably want not "dragging" but "moving". You can either reimplement mouseMoveEvent() or itemChange() for the item. Both will allow you to control the position of the item when it is moved with the mouse.

zgulser
2nd July 2009, 10:45
Hi ,

I see but for example what if I want to move the item in x axis with a restricted range let's say between x = 10 and x = -20 ?

wysota
2nd July 2009, 10:56
Read the docs of QGraphicsItem::itemChange()

zgulser
2nd July 2009, 13:23
I know that and I already read the docs. I try to look at docs before I send threads here.

Anyway, this is ok for rectangular shapes but what I want to really achieve is shapes like polygons or circle.

I think I need something like "closest point" to the shape when I make my item move on the shape in order to keep the item's position on the regarding shape.

Thanks in advance

wysota
2nd July 2009, 13:40
You can use any math calculations you want. The shape doesn't matter. Remember you always have the boundingRect() you can work with.

zgulser
3rd July 2009, 07:59
Hi again,

But if we can only play with the bounding rectangle, how can we perform this movement on a polygon since the bounding rectangle always returns a QRectF?

I will be pleased if you can give me more tips about it.

Thanks in advance

wysota
3rd July 2009, 12:14
I don't understand the problem. Could you elaborate?

zgulser
6th July 2009, 14:21
Sorry for the late answer first,

Yes I can. First of all, think I have a polygonal path for the mouse to be moved on the view. Then please see the following shape I've sent.

In this shape 1,2 and 3 are the example valid mouse locations. Suppose that the white background is the view(or scene) and the polygonal path is the path that the mouse can only move on.

wysota
6th July 2009, 14:31
I really don't see what is so problematic. Did you try using itemChange() as I told you? You can check there if the new position of the item is on the polyline and if not, snap it to the desired position. The fact that the example uses a rectangular area doesn't mean you can only snap to rectangular areas.

zgulser
6th July 2009, 14:53
Hi again,

I'm of course using itemChange method. But the problematic case is about the snapping. I couldn't figure out how to snap actually. That's why I said I need a closest point to my polygonal path to snap in one of my previous posts in this thread. What should I do in order to catch the correct snap position when the mouse moves?

wysota
6th July 2009, 15:56
Either do maths manually (the formula is not complicated) and/or use what QLineF offers. In general you need to find a line that is perpendicular to the closest component of the polyline that goes through the point of the current position of the item and then find the intersection of the original line and the perpendicular and the result will be the desired position of the item.

Of course you can just cheat and find two points - one that has the same horizontal coordinate as the current point and lies on the desired line, the other that has the same vertical coordinate as the current point and lies on the desired line and then compare the points to the current point and pick the one that is closer to the current point as the new point of the item.

You will still need to detect which component of the polyline is the closest to the current point.

Edit: here is a diagram to illustrate