Results 1 to 4 of 4

Thread: How does QGraphicsItem::prepareGeometryChange() work?

  1. #1
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default How does QGraphicsItem::prepareGeometryChange() work?

    I'm not having a problem with prepareGeometryChange(), I just wish I understood how it works. I tried looking at the source, but it wasn't very helpful for me. According to the docs it "Prepares the item for a geometry change. Call this function before changing the bounding rect of an item to keep QGraphicsScene's index up to date. prepareGeometryChange() will call update() if this is necessary."

    Does "before" mean *right* before? Any time before? Does this mean I don't need to call update()?

    Here is an example of where it becomes confusing: Say I have a QGraphicsItem subclass whose boundingRect() function returns a rectangle that is a static class member. If I want update the static rectangle, do I have to call prepareGeometryChange() on every instance of the class, update the static rectangle, and then call update() on every instance?

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How does QGraphicsItem::prepareGeometryChange() work?

    I would say yes.
    Even though you've changed single rectangle, bounding rectangle for every item has changed.
    So every item that will have different bounding rect should call prepareGeometryChange().

    As to why you should do that, it's because scene may need to redraw some other items to avoid artifacts.
    You should do it before changing the rectangle but rectangle should be changed before flow control goes back to event loop after calling prepareGeometryChange().
    And there should be no need to call update() as scene should do it for you.

  3. #3
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: How does QGraphicsItem::prepareGeometryChange() work?

    What I don't understand is how the scene knows the bounding rectangle has changed, since there is no setter for the bounding rect, only a getter. Does this mean that after you call prepareGeometryChange() the scene begins polling for changes, by calling QGraphicsItem::boundingRect() and comparing that one with what it has stored?

    I think something you said might be the answer, if you could confirm it:

    "...rectangle should be changed before flow control goes back to event loop after calling prepareGeometryChange()"

    Does this mean that QGraphicsScene only polls for a change right before control goes back to the event loop (and of course, only if prepareGeometryChange() has been called)?

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

    Default Re: How does QGraphicsItem::prepareGeometryChange() work?

    Quote Originally Posted by wayfaerer View Post
    Does "before" mean *right* before? Any time before?
    Any time after you know the rect is going to change and before you relinquish control of the flow to the event loop.

    Does this mean I don't need to call update()?
    Correct.

    Say I have a QGraphicsItem subclass whose boundingRect() function returns a rectangle that is a static class member.
    A very odd situation.

    If I want update the static rectangle, do I have to call prepareGeometryChange() on every instance of the class, update the static rectangle, and then call update() on every instance?
    You just need to update the static variable and call prepareGeometryChange() on every instance of the class.

    Quote Originally Posted by wayfaerer View Post
    What I don't understand is how the scene knows the bounding rectangle has changed, since there is no setter for the bounding rect, only a getter.
    It calls boundingRect(). That's why prepareGeometryChange() is required -- it essentially tells graphics view to remove any cached values regarding the size of the object and requery them from the object again.

    Does this mean that after you call prepareGeometryChange() the scene begins polling for changes, by calling QGraphicsItem::boundingRect() and comparing that one with what it has stored?
    Basically yes.

    Does this mean that QGraphicsScene only polls for a change right before control goes back to the event loop (and of course, only if prepareGeometryChange() has been called)?
    The scene updates its structures when it decides they need updating. A rule of thumb is that it will happen when some event regarding the scene is being processed.
    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.


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

    wayfaerer (2nd May 2012)

Similar Threads

  1. Subclassing QGraphicsItem to work as a layer
    By kefir in forum Qt Programming
    Replies: 7
    Last Post: 10th March 2011, 09:42
  2. Why QGLFramebufferObject in QGraphicsItem don’t work?
    By Student3000 in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2010, 20:12
  3. QGraphicsItem::setToolTip does not work
    By Nadia in forum Qt Programming
    Replies: 2
    Last Post: 6th September 2009, 19:54
  4. Replies: 5
    Last Post: 17th February 2009, 04:35
  5. prepareGeometryChange problem
    By stefan in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2008, 09:30

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.