PDA

View Full Version : pySide How to parent positions of QGraphicsItem()?



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)

frankvw
21st October 2017, 15:36
OK, maybe this is a simpler example. How to make the red circle follow the green circle, when the green circle is moved with the mouse, in the example below. Just using inherited methods, without writing a whole new function. Is that possible in QGraphicsScene?

import sys
from PySide import QtCore, QtGui


class MyFrame(QtGui.QGraphicsView):
def __init__( self, parent = None ):
super(MyFrame, self).__init__(parent)

scene = QtGui.QGraphicsScene()
self.setScene(scene)
self.setFixedSize(500, 500)

# add elipse coords
x = 0
y = 0
w = 45
h = 45
pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.green))
brush = QtGui.QBrush(pen.color().darker(150))

pen2 = QtGui.QPen(QtGui.QColor(QtCore.Qt.red))
brush2 = QtGui.QBrush(pen2.color().darker(150))

item = scene.addEllipse(x, y, w, h, pen, brush)
item.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
item.setPos(0,0)

item2 = scene.addEllipse(x, y, w, h, pen2, brush2)
item2.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
item2.setPos(50,-50)

if ( __name__ == '__main__' ):
app = QtGui.QApplication([])
f = MyFrame()
f.show()
app.exec_()


12623