Results 1 to 2 of 2

Thread: pySide How to parent positions of QGraphicsItem()?

  1. #1
    Join Date
    Sep 2017
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default pySide How to parent positions of QGraphicsItem()?

    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

    app_running1.jpg

    app running, item moved

    app_running2.jpg

    modified code

    code.jpg


    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)

  2. #2
    Join Date
    Sep 2017
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: pySide How to parent positions of QGraphicsItem()?

    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_()


    Screenshot from 2017-10-21 14-17-15.png

Similar Threads

  1. Replies: 7
    Last Post: 25th August 2015, 07:39
  2. Replies: 3
    Last Post: 19th June 2014, 13:59
  3. Replies: 0
    Last Post: 30th April 2013, 17:32
  4. Replies: 7
    Last Post: 29th November 2010, 19:20
  5. Parent QGraphicsItem and QGraphicsItemGroup
    By jano_alex_es in forum Newbie
    Replies: 5
    Last Post: 29th July 2009, 08:17

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.