Results 1 to 3 of 3

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

  1. #1

    Question [Solved]How to avoid setParentItem() from changing the item position?

    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());

    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();
    item->setPos(item->mapToItem(new_parent, item->pos());

    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?!?

    Last edited by Dr.Poldo; 12th June 2009 at 10:59. Reason: updated contents

  2. #2
    Join Date
    Jun 2007
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    MacOS X Unix/X11 Windows

    Default Re: How to avoid setParentItem() from changing the item position?

    what happens when u set the parent first and then change the pos?

  3. #3

    Default Re: How to avoid setParentItem() from changing the item position?

    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->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.

Similar Threads

  1. Graphics item position after drag
    By Miihkali in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2009, 10:01
  2. position of an item in a QStringList
    By jaca in forum Newbie
    Replies: 4
    Last Post: 29th May 2008, 05:49
  3. Changing selected item color in non-current window.
    By Doug Broadwell in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2007, 07:09
  4. Replies: 3
    Last Post: 12th April 2006, 09:35
  5. how change the QListBox item position by pixel
    By roy_skyx in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2006, 01:34

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.