Results 1 to 11 of 11

Thread: QGraphicsItem::setCacheMode - how does it work?

  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QGraphicsItem::setCacheMode - how does it work?

    I have a QGraphicsScene with a custom graphics item that can have a very compute-intensive paint() method, depending on the number of points in the data it is fed (basically, it is drawing a number of lines, where the count can get to 7 or 8 million). I tried creating a QPainterPath / QGraphicsPathItem, but I run out of memory with paths of millions of lines. So, I was forced to implement the paint() method.

    The problem is, that when I try to use a QRubberBand-derived class to zoom in or otherwise interact with the scene, I get a paint event on the item for every mouse event. For an item that must draw 8 million lines, this is a non-starter, and makes zooming impossible.

    I have read other posts which talk about the use of pixmaps to grab widget contents during zoom operations. QGraphicsItem supports pixmap caching, and from the description, it seems that this would do what I want - once the item is initially drawn into the pixmap, subsequent painting is done from the pixmap instead of the paint() method.

    In the constructor of my item, I added a call to setCacheMode( QGraphicsItem:eviceCoordinateCache ). As a result, my paint() method is never called. I have tried adding calls to update() in other places in the code where the item's contents are changed, but nothing ever happens. Comment out the setCacheMode() line in the constructor, everything is painted as usual.

    So what is going on here? Are there some preconditions for the use of CacheMode?

    Any examples? I've really been beating my head on the wall over this one.

  2. #2
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem::setCacheMode - how does it work?

    Quote Originally Posted by d_stranz View Post
    I have tried adding calls to update() in other places in the code where the item's contents are changed, but nothing ever happens.
    Did you call prepareGeometryChange() in those places where "the item's contents are changed" ?

  3. The following user says thank you to totem for this useful post:

    d_stranz (18th May 2012)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsItem::setCacheMode - how does it work?

    Did you call prepareGeometryChange() in those places where "the item's contents are changed" ?
    Yes, I am pretty sure it is called, at least by the parent item.

    I have one update routine where all changes to the child item are processed, and I know I am calling QGraphicsItem::update() from there. I will also add a prepareGeometryChange() call as well, see if that makes a difference.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsItem::setCacheMode - how does it work?

    OK, I've added a call to prepareGeometryChange() in the main method where all item changes are updated, and have confirmed that it is hit and that the scene sets the item dirty. I left in the existing call to update() for good measure.

    Still no paint method call on the item, though. :-( The items that are children of my custom item *are* painted, so I know a paint event is being generated somewhere, but my item's paint() method doesn't get called.

    Been beating my head on the wall for too long over this. Anyone have any ideas?

  6. #5
    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: QGraphicsItem::setCacheMode - how does it work?

    Please prepare a minimal compilable example reproducing the problem.
    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.


  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsItem::setCacheMode - how does it work?

    That's exactly what I'm now doing. There's way too much code in my plotting class to put that up as an example, so I'll try to write something simpler. If that turns out to work correctly, I'll beat my head on the wall some more until I figure out where the difference lies.

  8. #7
    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: QGraphicsItem::setCacheMode - how does it work?

    Subclass QGraphicsRectItem, add some debug messages to this class's paint() method, add an instance of it to your scene and enable caching for it. If paint() gets called after changing the rect of the item, then the blame is in your item code. If it doesn't it's somewhere else (e.g. scene code?).
    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.


  9. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsItem::setCacheMode - how does it work?

    OK, did this, it works, my original class still doesn't. The good news is that when I rubberband over the new QGraphicsRectItem, its paint() method *isn't* being called, which is exactly what I was trying to achieve in my original class and which I had hoped caching would achieve.

    Time to dig into the code and figure out what's going wrong.

  10. #9
    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: QGraphicsItem::setCacheMode - how does it work?

    A shortlist to check (as always with graphics items):
    1. Is my bounding rect defined correctly (e.g. is it independent of the placement of the item)?
    2. Is my shape defined within the bounding rect?
    3. Is my paint() routine painting only within the bounding rect?
    4. Am I calling prepareGeometryChange() each time the bounging rect is going to change?
    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.


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

    d_stranz (1st June 2012)

  12. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsItem::setCacheMode - how does it work?

    1. Is my bounding rect defined correctly (e.g. is it independent of the placement of the item)?
    Thank you, thank you. That's where the problem is. My bounding rect was defined in world coordinates, which resulted in a positive width() but negative height(). I have yet to get it right, but at least the item's paint() method is now being called as expected, and caching is doing the right thing when I rubberband.

    I owe you a beer or six.

    Edit: YES! That was it. Tell me where to send the beer.
    Last edited by d_stranz; 1st June 2012 at 01:33.

  13. #11
    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: QGraphicsItem::setCacheMode - how does it work?

    Quote Originally Posted by d_stranz View Post
    Edit: YES! That was it. Tell me where to send the beer.
    Qt Developer Days 2012. Hopefully...
    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.


Similar Threads

  1. Replies: 3
    Last Post: 25th March 2012, 20:01
  2. Subclassing QGraphicsItem to work as a layer
    By kefir in forum Qt Programming
    Replies: 7
    Last Post: 10th March 2011, 09:42
  3. Replies: 7
    Last Post: 29th November 2010, 19:20
  4. Why QGLFramebufferObject in QGraphicsItem don’t work?
    By Student3000 in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2010, 20:12
  5. QGraphicsItem::setToolTip does not work
    By Nadia in forum Qt Programming
    Replies: 2
    Last Post: 6th September 2009, 19:54

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.