import sys from PyQt4 import QtGui class EllipseGroup(QtGui.QGraphicsItemGroup): def __init__(self): super(EllipseGroup, self).__init__() # Create three ellipse items ellipse1 = QtGui.QGraphicsEllipseItem(0, 0, 50, 50) ellipse2 = QtGui.QGraphicsEllipseItem(75, 0, 50, 50) ellipse3 = QtGui.QGraphicsEllipseItem(150, 0, 50, 50) # Add the ellipses to the group self.addToGroup(ellipse1) self.addToGroup(ellipse2) self.addToGroup(ellipse3) if __name__ == '__main__': app = QtGui.QApplication(sys.argv) scene = QtGui.QGraphicsScene() group = EllipseGroup() scene.addItem(group) view = QtGui.QGraphicsView(scene) view.show() sys.exit(app.exec_())
import sys from PyQt4 import QtGui, QtCore class TriangleGroup(QtGui.QGraphicsItemGroup): def __init__(self): super(TriangleGroup, self).__init__() # Create the three line items that form the triangle line1 = QtGui.QGraphicsLineItem(0, 0, 100, 0) line2 = QtGui.QGraphicsLineItem(0, 0, 50, 50) line3 = QtGui.QGraphicsLineItem(100, 0, 50, 50) # Add the line items to the group self.addToGroup(line1) self.addToGroup(line2) self.addToGroup(line3) if __name__ == '__main__': app = QtGui.QApplication(sys.argv) scene = QtGui.QGraphicsScene() group = TriangleGroup() group.setPos(50, 50) scene.addItem(group) view = QtGui.QGraphicsView(scene) view.show() sys.exit(app.exec_())This example creates a custom QGraphicsItemGroup that contains three QGraphicsLineItem objects. The three line segments form a triangle. The group is added to a QGraphicsScene and displayed using QGraphicsView. The setPos method is used to position the group on the scene. In conclusion, the PyQt4 package library provides a powerful set of tools for creating GUI desktop applications with Python. The QGraphicsItemGroup class can be used to group graphical items together in a scene for better organization and manipulation.