Results 1 to 3 of 3

Thread: why is z order not respected when items are parented [QGraphicsView]

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default why is z order not respected when items are parented [QGraphicsView]

    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.


    Qt 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_())
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: why is z order not respected when items are parented [QGraphicsView]

    As items are painted by the view, starting with the parent items and then drawing children, in ascending stacking order. The child item will inherit the stacking order of the parent (or it's parent's parent). In other words setting z order on any item is not effective if it has parent item.

    I don't think there is any other way other than implementing move event, by temporaryly elivating the z order of the item's parent. BTW will QGraphicsItemGroup work fo you.
    Last edited by Santosh Reddy; 31st August 2013 at 13:23.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    amleto (31st August 2013)

  4. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: why is z order not respected when items are parented [QGraphicsView]

    oh, item group looks handy. Let me try it out...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 2
    Last Post: 28th October 2016, 11:12
  2. Replies: 3
    Last Post: 13th April 2011, 06:43
  3. Rendering order of items in a QwtPlot
    By citocran in forum Qwt
    Replies: 1
    Last Post: 7th January 2011, 15:47
  4. The order of items in QTreeWidget
    By KK in forum Qt Programming
    Replies: 4
    Last Post: 27th February 2009, 04:54
  5. Drag and drop items in view to sort order
    By Big Duck in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2006, 19:43

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.