I have made an example to demonstrate my problem - code is below (sorry, it's PyQt).

Consider this arrangement where the small rectangles are children of the larger one that surrounds them
Capture.JPG

I have set the Z Value for all the small rects to 2.0. However, when you drag a small rect over a large rect that was instantiated after the small rect's parent, then the view/scene will render the small rect under the large one! This is annoying me since I always want small rects to be on top.

Does anyone know a work around?

I 'need' the parenting because the larger rects are also movable and I want the contained small rect to move with it. I don't think I should have resort to managing move events myself for this.


Code:

  1. import sys
  2. from PyQt4 import QtGui, QtCore
  3.  
  4. if __name__ == '__main__':
  5. app = QtGui.QApplication(sys.argv)
  6.  
  7. view = QtGui.QGraphicsView()
  8. view.setScene(QtGui.QGraphicsScene(view))
  9.  
  10. r1 = view.scene().addRect(50,50,50,50)
  11. r2 = view.scene().addRect(120,50,50,50)
  12. r3 = view.scene().addRect(190,50,50,50)
  13.  
  14. r1.setBrush(QtGui.QColor(50,20,20))
  15. r2.setBrush(QtGui.QColor(50,20,20))
  16. r3.setBrush(QtGui.QColor(50,30,20))
  17.  
  18. r1.setFlags(QtGui.QGraphicsItem.ItemIsMovable)
  19. r2.setFlags(QtGui.QGraphicsItem.ItemIsMovable)
  20. r3.setFlags(QtGui.QGraphicsItem.ItemIsMovable)
  21.  
  22. rr1 = view.scene().addRect(50,50,10,10)
  23. rr2 = view.scene().addRect(120,50,10,10)
  24. rr3 = view.scene().addRect(190,50,10,10)
  25.  
  26. rr1.setBrush(QtGui.QColor(250,200,200))
  27. rr2.setBrush(QtGui.QColor(200,250,200))
  28. rr3.setBrush(QtGui.QColor(200,200,250))
  29.  
  30. rr1.setFlags(QtGui.QGraphicsItem.ItemIsMovable | QtGui.QGraphicsItem.ItemIsSelectable)
  31. rr2.setFlags(QtGui.QGraphicsItem.ItemIsMovable | QtGui.QGraphicsItem.ItemIsSelectable)
  32. rr3.setFlags(QtGui.QGraphicsItem.ItemIsMovable | QtGui.QGraphicsItem.ItemIsSelectable)
  33. rr1.setZValue(2.0)
  34. rr2.setZValue(2.0)
  35. rr3.setZValue(2.0)
  36.  
  37. rr1.setParentItem(r1)
  38. rr2.setParentItem(r2)
  39. rr3.setParentItem(r3)
  40.  
  41. view.show()
  42.  
  43. sys.exit(app.exec_())