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?

    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.
    Last edited by Dr.Poldo; 12th June 2009 at 11:59. Reason: updated contents

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    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->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.

Similar Threads

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

Tags for this Thread

Bookmarks

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.