frankvw
19th October 2017, 16:04
Hi,
Using the diagramscene.py example of pySide/pyQt (same as QT C++ example), I wanted to know how to set a parent for QGraphicsItems() so that within a QGraphicsScene(), moving one will cause the other to follow. There are parent() and setParent() methods but they don't seem to form a parent relationship between objects, even if they don't error either. There are some parent like relationships set with the Arrow class via overides but this is overkill for my text label needs and is connected anyway to user selections. I need just a simple parent-child relationship set a creation time within the QGraphicsScene().
Here is the code below I have modified on the distribution example files, adding a DiagramTextItem() at the same time to a DiagramShapeItem (both subclasses of QGraphicsItem).
I get text added at the same position using the setPos() method but that's where I get stuck because my attempts at forming a parent relationship between item and textItem have so far failed. So that when the app runs, I want moving a shape to also move it's associated text graphic. Here are some screen grabs to clarify.
Cheers!
app running, item inserted
12615
app running, item moved
12616
modified code
12617
def mousePressEvent(self, mouseEvent):
if (mouseEvent.button() != QtCore.Qt.LeftButton):
return
if self.myMode == self.InsertItem:
item = DiagramItem(self.myItemType, self.myItemMenu)
item.setBrush(self.myItemColor)
self.addItem(item)
item.setPos(mouseEvent.scenePos())
self.itemInserted.emit(item)
# added here another TextItem object at same time
textItem = DiagramTextItem("TextLabel")
textItem.setFont(self.myFont)
textItem.setZValue(1000.0)
textItem.lostFocus.connect(self.editorLostFocus)
textItem.selectedChange.connect(self.itemSelected)
self.addItem(textItem)
textItem.setDefaultTextColor(self.myTextColor)
textItem.setPos(item.pos())
#textItem.setParentItem(item)
# setPos(item.pos()) correctly adds textItem to shape but need to parent it
# so that when "item" parent is selected and moved in QGraphicsScene, the
# additional textItem will be moved along with it
elif self.myMode == self.InsertLine:
self.line = QtGui.QGraphicsLineItem(QtCore.QLineF(mouseEvent.s cenePos(),
mouseEvent.scenePos()))
self.line.setPen(QtGui.QPen(self.myLineColor, 2))
self.addItem(self.line)
elif self.myMode == self.InsertText:
textItem = DiagramTextItem()
textItem.setFont(self.myFont)
textItem.setTextInteractionFlags(QtCore.Qt.TextEdi torInteraction)
textItem.setZValue(1000.0)
textItem.lostFocus.connect(self.editorLostFocus)
textItem.selectedChange.connect(self.itemSelected)
self.addItem(textItem)
textItem.setDefaultTextColor(self.myTextColor)
textItem.setPos(mouseEvent.scenePos())
self.textInserted.emit(textItem)
super(DiagramScene, self).mousePressEvent(mouseEvent)
Using the diagramscene.py example of pySide/pyQt (same as QT C++ example), I wanted to know how to set a parent for QGraphicsItems() so that within a QGraphicsScene(), moving one will cause the other to follow. There are parent() and setParent() methods but they don't seem to form a parent relationship between objects, even if they don't error either. There are some parent like relationships set with the Arrow class via overides but this is overkill for my text label needs and is connected anyway to user selections. I need just a simple parent-child relationship set a creation time within the QGraphicsScene().
Here is the code below I have modified on the distribution example files, adding a DiagramTextItem() at the same time to a DiagramShapeItem (both subclasses of QGraphicsItem).
I get text added at the same position using the setPos() method but that's where I get stuck because my attempts at forming a parent relationship between item and textItem have so far failed. So that when the app runs, I want moving a shape to also move it's associated text graphic. Here are some screen grabs to clarify.
Cheers!
app running, item inserted
12615
app running, item moved
12616
modified code
12617
def mousePressEvent(self, mouseEvent):
if (mouseEvent.button() != QtCore.Qt.LeftButton):
return
if self.myMode == self.InsertItem:
item = DiagramItem(self.myItemType, self.myItemMenu)
item.setBrush(self.myItemColor)
self.addItem(item)
item.setPos(mouseEvent.scenePos())
self.itemInserted.emit(item)
# added here another TextItem object at same time
textItem = DiagramTextItem("TextLabel")
textItem.setFont(self.myFont)
textItem.setZValue(1000.0)
textItem.lostFocus.connect(self.editorLostFocus)
textItem.selectedChange.connect(self.itemSelected)
self.addItem(textItem)
textItem.setDefaultTextColor(self.myTextColor)
textItem.setPos(item.pos())
#textItem.setParentItem(item)
# setPos(item.pos()) correctly adds textItem to shape but need to parent it
# so that when "item" parent is selected and moved in QGraphicsScene, the
# additional textItem will be moved along with it
elif self.myMode == self.InsertLine:
self.line = QtGui.QGraphicsLineItem(QtCore.QLineF(mouseEvent.s cenePos(),
mouseEvent.scenePos()))
self.line.setPen(QtGui.QPen(self.myLineColor, 2))
self.addItem(self.line)
elif self.myMode == self.InsertText:
textItem = DiagramTextItem()
textItem.setFont(self.myFont)
textItem.setTextInteractionFlags(QtCore.Qt.TextEdi torInteraction)
textItem.setZValue(1000.0)
textItem.lostFocus.connect(self.editorLostFocus)
textItem.selectedChange.connect(self.itemSelected)
self.addItem(textItem)
textItem.setDefaultTextColor(self.myTextColor)
textItem.setPos(mouseEvent.scenePos())
self.textInserted.emit(textItem)
super(DiagramScene, self).mousePressEvent(mouseEvent)