PDA

View Full Version : [Solved]How to avoid setParentItem() from changing the item position?



Dr.Poldo
12th June 2009, 10:15
Hi,
i'm using QGraphicsItem and some animations, i have a problem when i change the parent of an item because also it's position is changed. That's due to the fact that the position is related to the parent position, so i tried this:

item->setPos(item->mapToItem(new_parent, item->pos());
item->setParentItem(new_parent);

but it seams like setParentItem occurs before setPos() and it updates the scene with the item in the wrong position.

The only solution i found is to block all scene signals, set the parent , set the position, unlock the signals and update:

QPointF position = item->pos();
scene()->blocksignals(true);
item->setPos(item->mapToItem(new_parent, item->pos());
item->setParentItem(new_parent);
scene()->blocksignals(false);

that does the job but i'm looking for something more elegant than block all signals, also because i have other items on the scene and that would block their signal as well.

Any suggestion?!?

Thanks.

nish
12th June 2009, 10:28
what happens when u set the parent first and then change the pos?

Dr.Poldo
12th June 2009, 10:55
Thanks for the reply but luckly I solved the problem, i don't understand the behavior of mapToItem method, after some std::cout i realize that the position i was setting was wrong so i write this:

item->setParentItem(new_parent);
item->setPos(item->pos() - new_parent->pos());

and that works great. And yes you're right, setPos can be placed after setParentItem. All the problem was the map function.:D