PDA

View Full Version : Problem with QGraphicsItem coordinades



jano_alex_es
29th July 2009, 13:30
Hi,

I have few QGraphicsLineItems linked in a line one after the other. All of them have a QGraphicsItem as a parent, some "parents" have just one child and some few.

I want to update the positions of each line eachtime the others are moved. Until now it worked fine using "mapFromItem" in the starting point and the last point of each line, with the coords of the following and the previous items.

But when I create a new QGraphicsLineObject at the end of the queue, and if the previous parent object has been moved, the coordenade system is moved as well and "mapFromItem" is not right (it's moved a bit more, I thinks that's because the parent has been moved and its coordenade axis as well).

I tried with "mapToScene", but it doesn't work fine:



QPointF newPos = mapToScene(my_OLDItem->line().p2());
my_NEWItem->setPos(NewPos); //my_NEWItem is a QGraphicsPixmapItem


what should I map, and where? any idea? thanks!

wysota
29th July 2009, 14:03
Are the items connected using parent-child relationship? If you wish to update their scene positions then they shouldn't be... It doesn't make much sense otherwise.

jano_alex_es
30th July 2009, 08:06
Nop, the QGraphicsLineItem are connected graphically (the last point is in the same position of the first point of the next one).

Thus, all of them are child of a QGraphicsPathItem. I have several of those items and all of them are parent of one or more QGraphicsLineItem.

wysota
30th July 2009, 08:09
So what's the problem? Reimplement itemChange() for your item and move all the associated item when the current item moves. If one item moved 10 pixels right and 7 down, all others have to move 10 pixels right and 7 down - you don't have to map anything. Furthermore if they have the same parent, they "live" in the same coordinate system which makes the whole thing even easier.

wagmare
30th July 2009, 09:11
need some clarification ...
just query ... why QGraphicsItemGroup cant do this ... when an item in the group moved the whole group items will move respectively ..?

jano_alex_es
30th July 2009, 09:29
wysota: I forgot... I also have QGraphicsPixmap items, linked with the LineItems and inside the pathItems as well. So my big issue was with them.

I was dealing with "setScenePos" and "mapToScene", but they didn't work... now I'm working with "setPos" and "mapToParent/mapFromParent" more or less succesfully.


wagmare: that's because my program... I need to display a QPath and if I use a QGraphisGroup I need to create a QPath and calculate the points of the QPath manually when is moved and paint a qpath on the screen (because it moves the items inside, but the paints to be drawn as a QPath need to be calculated each frame and it's not worth). I do prefer to create a QGraphicsPathItem, move it as a normal item and move the items inside automatically because they are its childs.

wysota
30th July 2009, 09:51
wysota: I forgot... I also have QGraphicsPixmap items, linked with the LineItems and inside the pathItems as well. So my big issue was with them.

I was dealing with "setScenePos" and "mapToScene", but they didn't work... now I'm working with "setPos" and "mapToParent/mapFromParent" more or less succesfully.
If they have a common parent then you shouldn't be mapping to scene. There should be no mapping at all.

In general it would be easiest to place all the movable items into one item group and make that group movable instead of the child items themselves. If you tell the parent to handle events from its children, it should work without any additional code. And no need to react on position changes.

jano_alex_es
30th July 2009, 10:42
That works fine... but it does not when they are childs of different parent items.

Imagine we have:

QLineItem at position (general position, in a paper, not in qt) from 0,0 to 20,0

QLineItem from 20, 0 to 40, 40

QLineItem from 40, 40 to 80, -30

all are childs of the same QItem

and, after, connected, I have a QPixmapItem at pos 80,30, and it's a child of another QItem.

I was having problems mapping these coordenades, mapToScene of the line().p2() and map from scene at QPixmapItem.

Fortunatly, in my program each QPixmapItem needs to be surrounded by three qLineItem, being all of them childs of the same parent, so I can pass the coords mapfromItem between the qlineItems that are childs of different parents and map to parent between the items of the same parent.