別のという名前の子である という名前QGraphicsItem
があります。親がないように、親を外す必要があります。child_item
QGraphicsItem
parent_item
child_item
parent_item
child_item
しかしchildItem.setItemParent(None)
、スクリプトを試してみるとクラッシュします。
どうやらこれはQGraphicsItem
、このように a の親を削除すると、アイテムが Qt に返されるためです...
今のところ、 を作成したglobal_parent
QGraphicsItem
ので、親を外す必要がある項目がある場合は、 の下で親にするだけで、親があるglobal_parent
場合、コードはQGraphicsItem
親global_parent
を持たないように動作しますが、より良い解決策が必要です。
アイデアはありますか?
私のコードの一部:
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)
setItemParent を呼び出してアイテムの親を別のアイテムに設定しても機能するので、当面は汎用の親を使用します。