PDA

View Full Version : Problem with mapping positions into the scene



jano_alex_es
8th September 2009, 10:48
Hi,

I have a problem with the position of the graphics items in my scene and I don't know how to solve it.

I have one QGraphicsLineItem on my scene and, when I click on it, it creates a new one. It should draw a horizontal line starting in the last point of the previous item and finishing 30px to the right.

All of them are childs of biggers QGraphicsItems, pressing a button I enable the user to move those big parent items (moving the childs as well). Some of those objects have two, three or more of the small QGraphicsLineItem.

So far, I was doing it with:


//pseudocode
QGraphicsLineItem* newItem = new QGraphicsLineItem();
newItem->setParentItem(parentItem);

QPointF prevItemEndingPoint = previousItem.line().p2();
QPointF startingPoint = newItem->mapFromItem(previousItem, prevItemEndingPoint );
QPointF endingPoint = startingPoint;
endingPoint.setX(startingPoint.rx() + 30);

newItem.setLine(previousItem, endingPoint);

It worked fine until now. I've just realized it doesn't work when the I create an item of a different parent and I have moved one the previous parent before, like for instance:

http://img171.imageshack.us/img171/7564/13262898.jpg

http://img35.imageshack.us/img35/6559/27455726.jpg

Now I want to create a new GraphicsItem, so I click in A (pink object is desired, but green is created)
http://img36.imageshack.us/img36/2682/31002329.jpg


how I can map those coords? I think I have tried everything...

thanks!

bunjee
10th September 2009, 20:22
I have one QGraphicsLineItem on my scene and, when I click on it, it creates a new one


I create an item of a different parent

Why do you change its parent ? All your GraphicsItem should have the same parent.

see http://doc.trolltech.com/4.5/graphicsview.html#the-graphics-view-coordinate-system

jano_alex_es
11th September 2009, 08:05
Because they are grouped, each parent is a sort of group of other items. So if you press a button the parent is shown, the childs are hidden and you can move that group and only that group and its elements.

QGraphicsItemGroup is not an option because the parent is a "QGraphicsPathItem" (it has to be this way)

wysota
11th September 2009, 08:33
Because they are grouped, each parent is a sort of group of other items. So if you press a button the parent is shown, the childs are hidden and you can move that group and only that group and its elements.

QGraphicsItemGroup is not an option because the parent is a "QGraphicsPathItem" (it has to be this way)

You can group items not only using a parent-child relationship. In your situation I'd advise to have all items within the same parent as well. Just make the "group item" hold pointers to all its members and when the group item moves, move the members along. Otherwise you'll be constantly recalculating relative positions of items which I see is hard for you to cope with.