Results 1 to 7 of 7

Thread: QGraphicsItem pos() and setpos()

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2008
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem pos() and setpos()

    Thank you wysota for the explanation.
    I still have a question. If I have several rectangles on a scene, how would I save the locations and be able to recreate them again? The boundingRect() doesn't seem to change when the items are moved around the scene. Thanks.

  2. #2
    Join Date
    May 2008
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem pos() and setpos()

    Looks like sceneBoundingRect () was the right method to use instead of boundingRect().

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem pos() and setpos()

    Not really. You should save boundingRect(), pos() and transformation matrix to have a complete set of data needed to recreate the item. sceneBoundingRect() will return wrong values once you start rotating items.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2008
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem pos() and setpos()

    Thanks Wysota.
    Is it really a common practice to save boundingRect(), pos() and transformation matrix along with your graphicsitem? I was just thinking if calculations could be done with just the graphicsitem itself. So just to clarify, if I create a graphicsitem, I would need to track its current position (pos ,boundingRect etc) on a separate data structure (maybe) and then retrieve these values to do future calculations?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem pos() and setpos()

    I don't know what you are trying to achieve so it is hard to answer this question. The boundingRect is a coordinate space of the item and the item itself always operates in its own coordinate space. So if you rotate a parent item, all its child items get rotated although they still think they have the same coordinates as before.

    Let's consider an example - the Earth spins around the Sun so absolute (actually relative to the Solar System) coordinates of each place on Earth change all the time. But we don't care as we are subject to the same changes ourselves (we're bound to Earth's coordinate system) so we can still use Earth's coordinates - planes can reach their cities of destination without compensating for Earth's trip around the Sun or around its own axis. Now let's have a broader look - the Sub spins around the centre of the Milky Way galaxy so it's coordinates change all the time and these changes also influence absolute coordinates of places on Earth. But people on Earth don't have to care about it, they always use their local (Earth's) coordinate space. That's the same with Graphics View - the item only "sees" its own coordinates relative to its parent just like people see world from their own perspective relative to their "parent object" which is Earth. If you want to recreate your position on Earth, you need to know the lat. and long. coordinates and height above sea level (equivalent of QGraphicsItem::pos()) but you also need to know how tall you are, which side you are facing, what is the colour of your skin, your name, etc. You have to store all that data somewhere - otherwise it would not be possible to distinguish between you and some other person if (s)he occupied the same coordiantes as you. Those attributes are the item's boundingRect, color, brush, etc. The more precisely you want to recreate an item, the more data about it you need to store. It is up to you how much data to store and what to recreate afterwards.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    jimc1200 (6th May 2009)

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
  •  
Qt is a trademark of The Qt Company.