Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving a QGraphicsItem's parent
    primarykey
    data
    text
    <p>I have a <code>QGraphicsItem</code> called <code>child_item</code> that is a child of another <code>QGraphicsItem</code> called <code>parent_item</code>. I need to unparent <code>child_item</code> from <code>parent_item</code> so that <code>child_item</code> has no parent.</p> <p>But when I try <code>childItem.setItemParent(None)</code> my script crashes. </p> <p>Apparently this is because when you remove a <code>QGraphicsItem</code>'s parent in this way the item is returned to Qt...</p> <p>For now I've just created a <code>global_parent</code> <code>QGraphicsItem</code> so that if any item needs to be unparented I will simply parent it under the <code>global_parent</code> and if a <code>QGraphicsItem</code> has the parent <code>global_parent</code> my code will act like it doesn't have a parent but I would like a better solution.</p> <p>Any ideas please?</p> <h2>Some of my code:</h2> <pre><code>POINT_SIZE = 7 class Test_Box(QGraphicsItem): # Constants WIDTH = 50 * POINT_SIZE HEIGHT = 13 * POINT_SIZE RECT = QRectF(0, 0, WIDTH, HEIGHT) CORNER_RADIUS = 1.5 * POINT_SIZE def __init__(self, position, parent=None): super(Test_Box, self).__init__(parent) # Settings self.setFlags( self.flags() | QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsFocusable | QGraphicsItem.ItemSendsScenePositionChanges ) self.setPos(position) def boundingRect(self): return Test_Box.RECT def paint(self, painter, option, widget): # Draw Box brush = QBrush() painter.setBrush(brush) painter.drawRoundedRect(Test_Box.RECT, Test_Box.CORNER_RADIUS, Test_Box.CORNER_RADIUS) def itemChange(self, change, variant): super(Test_Box, self).itemChange(change, variant) if change == QGraphicsItem.ItemScenePositionHasChanged: self.setParentItem(None) return QGraphicsItem.itemChange(self, change, variant) </code></pre> <p>Calling setItemParent and setting the item's parent to another item does work though so I'm using a generic parent in the meantime.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload