I am trying to use the QGraphicsItemGroup
class to constrain a group of objects to move by the same amount in one (e.g. the vertical) direction as shown in the diagram below, where the vertical movement of the item B (shown by a red arrow) is shared by items A, C and D, but not its horizontal movement (which is shown by the blue arrows). See also the code below:
import sys
from PySide2.QtCore import Qt
from PySide2.QtGui import QBrush
from PySide2.QtWidgets import QApplication , QGraphicsScene , QGraphicsView , QGraphicsItem , QGraphicsItemGroup , QGraphicsEllipseItem
if __name__=="__main__":
app = QApplication(sys.argv)
graphicsView = QGraphicsView()
graphicsView.setSceneRect( -50 , -50 , 300 , 100 )
scene = QGraphicsScene(graphicsView)
graphicsView.setScene(scene)
graphicsItemGrp = QGraphicsItemGroup()
graphicsItemGrp.setFlag( QGraphicsItem.ItemIsMovable )
graphicsItemGrp.setHandlesChildEvents( False )
brush = QBrush( Qt.black )
item1 = QGraphicsEllipseItem( 0 , 0 , 20 , 20 )
item1.setFlag(QGraphicsItem.ItemIsMovable)
item1.setCursor( Qt.SizeAllCursor )
item1.setBrush( brush )
item2 = QGraphicsEllipseItem( 50 , 0 , 20 , 20 )
item2.setFlag(QGraphicsItem.ItemIsMovable)
item2.setCursor( Qt.SizeAllCursor )
item2.setBrush( brush )
item3 = QGraphicsEllipseItem( 100 , 0 , 20 , 20 )
item3.setFlag(QGraphicsItem.ItemIsMovable)
item3.setCursor( Qt.SizeAllCursor )
item3.setBrush( brush )
item4 = QGraphicsEllipseItem( 150 , 0 , 20 , 20 )
item4.setFlag(QGraphicsItem.ItemIsMovable)
item4.setCursor( Qt.SizeAllCursor )
item4.setBrush( brush )
graphicsItemGrp.addToGroup( item1 )
graphicsItemGrp.addToGroup( item2 )
graphicsItemGrp.addToGroup( item3 )
graphicsItemGrp.addToGroup( item4 )
scene.addItem( graphicsItemGrp )
graphicsView.show()
sys.exit(app.exec_())
I am not certain that QGraphicsItemGroup
is the way to go but its the most promising approach that I've been able to find.
A QGraphicsItemGroup is a special type of compound item that treats itself and all its children as one item (i.e., all events and geometries for all children are merged together). It's common to use item groups in presentation tools, when the user wants to group several smaller items into one big item in order to simplify moving and copying of items.
Source Qt 5.13 docs