PDA

View Full Version : Handling Objects coordinates related to another object.



alitoh
30th March 2011, 22:08
So ...

I need to create objects and position them in direct relation to a certain object already in the scene. In fact, this item is a parent of said new item-to-be-created.

I have this:

A dialog window.
a scene object.
a view view object.
a parent object.
many child objects.

I want each child I create to use its parameters (ones that I pass to it when instantiated) and create itself taking as 0,0 the parent object's 0,0, not the scene's 0,0.

Example;
I have a parent Square drawn at (4,4, 1000, 1000). Now I want instantiate new items, childs, and if I pass a (2, 2, 10, 10) as parametere, to have it be drawn in what would be (4 + 2, 4 + 2, 10, 10) from a scenes perspective or just (2, 2, 10, 10) from the parent's perspective.

I'm trying to do something like

parent = new QGraphcisItem();
child = new QGraphicsItem();

child->setParentItem(parent);

the thing is, now I don't know how to proceed after this, or if this is even alright.

The reason why I want to attach childs to a parent is because I can just make the parent's size escale with the scene's scaling-resizing and have the children react with their parent and it's own coordinates and proportions.

Think of a graph in a window. If your window changes sizes, the graph rescales everything in accordance with the window's new parameters of height and width and, with that, so get the children to change.

wysota
31st March 2011, 11:41
I want each child I create to use its parameters (ones that I pass to it when instantiated) and create itself taking as 0,0 the parent object's 0,0, not the scene's 0,0.
That's exactly what happens by default if one item is parent to the other. If it doesn't work out of the box, you must have done something wrong.