from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsEllipseItem, QGraphicsItemGroup # create a scene scene = QGraphicsScene() # add some circles to the scene circle1 = QGraphicsEllipseItem(0, 0, 50, 50) circle1.setBrush(Qt.red) scene.addItem(circle1) circle2 = QGraphicsEllipseItem(100, 100, 50, 50) circle2.setBrush(Qt.blue) scene.addItem(circle2) # create a group of the circles group = QGraphicsItemGroup() group.addToGroup(circle1) group.addToGroup(circle2) # add the group to the scene scene.addItem(group) # create a view and set the scene view = QGraphicsView(scene) # show the view view.show()
from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsEllipseItem, QGraphicsItemGroup # create a scene scene = QGraphicsScene() # add some circles to the scene circle1 = QGraphicsEllipseItem(0, 0, 50, 50) circle1.setBrush(Qt.red) scene.addItem(circle1) circle2 = QGraphicsEllipseItem(100, 100, 50, 50) circle2.setBrush(Qt.blue) scene.addItem(circle2) # create a group of the circles group = QGraphicsItemGroup() group.addToGroup(circle1) group.addToGroup(circle2) # remove the second circle from the group group.removeFromGroup(circle2) # add the group to the scene scene.addItem(group) # create a view and set the scene view = QGraphicsView(scene) # show the view view.show()This example removes the second circle from the group and then adds the modified group to the scene. As a result, only the first circle is displayed in the view. In conclusion, PyQt5 QtWidgets QGraphicsItemGroup is a useful module for managing and grouping QGraphicsItems. It is a part of the PyQt5 library, which is widely used for creating GUI applications in Python.