Results 1 to 5 of 5

Thread: Question about QGraphicsItem and cache mode

  1. #1
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Question about QGraphicsItem and cache mode

    I have a class that inherits QGraphicsItem:
    Qt Code:
    1. class DisplayItem(QGraphicsItem):
    2. def __init__(self, parent):
    3. self.setAcceptHoverEvents(True)
    4. self.setFlag(QGraphicsItem.ItemIsMovable)
    5. self.setFlag(QGraphicsItem.ItemIsSelectable)
    6. self.setCacheMode(QGraphicsItem.DeviceCoordinateCache)
    7. def boundingRect(self):
    8. adjust = 2.0
    9. return QRectF(-10 - adjust, -10 - adjust, 23 + adjust, 23 + adjust)
    10. def shape(self):
    11. path = QPainterPath()
    12. path.addEllipse(-10, -10, 20, 20)
    13. return path
    14. def paint(self, painter, option, widget):
    15. painter.setPen(Qt.NoPen)
    16. painter.setBrush(Qt.darkGray)
    17. painter.drawEllipse(-7, -7, 20, 20)
    18. gradient = QRadialGradient(-3, -3, 10)
    19. color = QColor(QColor.fromHsv(60, 255, 255))
    20. if self.isSelected():
    21. color = QColor(Qt.white)
    22. painter.setBrush(QBrush(color))
    23. painter.setPen(QPen(Qt.black, 0))
    24. painter.drawEllipse(-10, -10, 20, 20)
    25. def mousePressEvent(self, event):
    26. self.scene().clearSelection()
    27. self.setSelected(True)
    28.  
    29. self.update()
    30.  
    31. self.bringToFront()
    32.  
    33. QGraphicsItem.mousePressEvent(self, event)
    To copy to clipboard, switch view to plain text mode 
    I have abunch of these QGraphicsItems in a QGraphicsScene displayed on screen through a QGraphicsView. The QGraphicsItem normally looks like a yellow circle with a black edge. When I click on the QGraphicsItem, it turns into a white circle with a black edge.
    When I click on a different QGraphicsItem, the first item is supposed to revert back to being yellow and the new item is supposed to turn white. This does not happen. The old item stays white and the new item turns white.
    However, if I comment out:
    Qt Code:
    1. self.setCacheMode(QGraphicsItem.DeviceCoordinateCache)
    To copy to clipboard, switch view to plain text mode 
    eevrything works the way it's supposed to. The new clicked on item turns white, and the old one turns back to yellow. I thought in this mode, the cache was regenerated everytime the item changes. So how come my items don't change?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Question about QGraphicsItem and cache mode

    Quote Originally Posted by di_zou View Post
    I thought in this mode, the cache was regenerated everytime the item changes. So how come my items don't change?
    Well, yes, but clicking an item is not changing an item.

    Quoting the documentation:
    If the item is transformed directly or indirectly, the cache will be regenerated automatically.
    Which makes sense since this type of caching is used for the best quality rendering (which is costly)

  3. #3
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question about QGraphicsItem and cache mode

    So I guess changing the color isn't a transform? Is there any way to use this cache mode and do what I want it to do? ie update the chache everytime the color changes or everytime I click on it?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Question about QGraphicsItem and cache mode

    Quote Originally Posted by di_zou View Post
    So I guess changing the color isn't a transform?
    No

    Is there any way to use this cache mode and do what I want it to do? ie update the chache everytime the color changes or everytime I click on it?
    Not with this cache mode, it is very specific (hence why it is fast). Use QGraphicsItem::ItemCoordinateCache or write your own cache.

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

    di_zou (13th July 2010)

  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: Question about QGraphicsItem and cache mode

    I'm not sure you are correct. I think that calling update() on an item should invalidate the cache regardless of the mode. To me it seems the other item is simply not deselected (at least I don't see any code for that in the snippet above) and that's why it doesn't come back to its original state.
    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. QGraphicsItem pos() method question!
    By zgulser in forum Qt Programming
    Replies: 0
    Last Post: 20th August 2009, 19:56
  2. Simple QGraphicsItem question
    By godmodder in forum Qt Programming
    Replies: 2
    Last Post: 13th November 2008, 17:34
  3. Replies: 8
    Last Post: 10th October 2007, 18:20
  4. QGraphicsItem move question
    By mhoover in forum Qt Programming
    Replies: 2
    Last Post: 7th December 2006, 19:01
  5. QtFileDialog: Mode Question!
    By Mokster in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2006, 21:41

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.