Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

😂 How do you move a parent QGraphicsObject precisely by moving its child?



  • The default is that the movements in the parent move the child because the child is in the parent's local coordinates always.

    I want the reverse of this.

    I know I can get it working with a position_changed signal, but I wanted to try it now with a sceneEventFilter. It's not working

    Insert obvious, naive code that listens for GraphicsSceneMousePress -and- GraphicsSceneMouseMove in event.type() to no avail.

    What is the canonical way to accomplish this?

    I would add that, the children in question are always within the parent's bounding rect.



  • from PyQt5.QtWidgets import (QGraphicsObject, QMainWindow, QGraphicsTextItem, 
                                 QApplication, QGraphicsScene, QGraphicsView)
    
    class A(QGraphicsObject):
        def __init__(self):
            super().__init__()
            #self.setFiltersChildEvents(True)
            
        def boundingRect(self):
            return self.childrenBoundingRect()
        
        def paint(self, painter, option, widget):
            painter.drawRect(self.boundingRect())
            
        
    class B(A):
        def __init__(self, text):
            super().__init__()
            self.text = QGraphicsTextItem(text)
            self.text.setParentItem(self)
            #self.text.installSceneEventFilter(self)
            
    class C(A):
        def __init__(self, text):
            super().__init__()
            self.b = B(text)
            self.b.setParentItem(self)
            self.setFlags(self.ItemIsMovable)
            
    if __name__ == '__main__':
        import sys
        app = QApplication([])
        
        wind = QMainWindow()
        wind.show()
        
        view = QGraphicsView()
        scene = QGraphicsScene()
        view.setScene(scene)
        wind.setCentralWidget(view)
        
        c = C('See?')
        scene.addItem(c)
        
        sys.exit(app.exec_())
    

    Here's the min example code, but it works... so...

    Any idea why this minimal example should work as I have described? What is causing it to work?



  • K got it to work in my code now. Thanks, newbleets!


Log in to reply