1. Overview
QGraphicsViewandQGraphicsSceneIt is the core class of the Qt Graphics View Framework for building high-performance, interactive 2D graphical interfaces.
Core division of labor:
-
QGraphicsScene: Manage graph items in the scene (
QGraphicsItem
), handles events and coordinate systems. - QGraphicsView: As the viewport for observing the scene, it provides view transformation functions such as zoom, translation, and rotation.
Applicable scenarios:
- Complex drawings (such as CAD tools)
- Game Development (2D Scene)
- Data visualization (charts, flow charts)
- Interactive graphical interface (components that can be dragged and edited)
2. Core components and relationships
Component level
QGraphicsView (view) └── QGraphicsScene (Scene) └── QGraphicsItem (Graphics Items:rectangle、oval、text、Custom items, etc.)
Coordinate system difference
- Scene coordinates: The global coordinate system of the scene (or origin is in the center of the scene or in the custom position).
- View coordinates: The coordinate system of the view window (original point is in the upper left corner).
- Item coordinates: The local coordinate system of each graphic item itself.
3. Basic usage steps
Create scenes and views
from import QGraphicsView, QGraphicsScene, QApplication from import Qt scene = QGraphicsScene() # Create a sceneview = QGraphicsView(scene) # Create a view and bind the scene() # Anti-aliasing(800, 600) ()
Add graphics items to the scene
# Add rectangle (position, size, color)rect = (0, 0, 100, 50, , ) # Add texttext = ("Hello Graphics", QFont("Arial", 12)) (50, 50) # Add an ellipseellipse = (200, 100, 80, 60, )
4. Core functions and practical cases
Interactive graphics items (drag, rotate)
class MovableRect(QGraphicsRectItem): def __init__(self, x, y, w, h): super().__init__(x, y, w, h) () # Allow drag and drop () # Allow to select () # Add movable rectangle to the scenemovable_rect = MovableRect(300, 200, 80, 40) (movable_rect)
View operations (zoom and pan)
# Mouse wheel zoomdef wheelEvent(self, event): factor = 1.2 if ().y() > 0 else 0.8 (factor, factor) # Right-click drag and pan() # Set drag mode
Custom graphics items (draw arrows)
class ArrowItem(QGraphicsItem): def boundingRect(self): return QRectF(-10, -5, 20, 10) # Define item boundaries def paint(self, painter, option, widget): (QPen(, 2)) (0, 0, 10, 0) # Arrow body (10, 0, 5, -5) # Arrow tip (10, 0, 5, 5) arrow = ArrowItem() (400, 300) (arrow)
Combining animation and graphics items
# Use QPropertyAnimation to move graphics itemsfrom import QPropertyAnimation anim = QPropertyAnimation(arrow, b"pos") (2000) (QPointF(400, 300)) (QPointF(500, 400)) () ()
V. Advanced features
Collision detection
# Detect the collision between rectangles and other itemscolliding_items = () for item in colliding_items: () # Highlight collision item
Combination items (QGraphicsItemGroup)
group = QGraphicsItemGroup() (rect) (text) (45) # Overall rotation 45 degrees(group)
Scene event processing
class CustomScene(QGraphicsScene): def mousePressEvent(self, event): if () == : print(f"Scene Click Location:{()}") super().mousePressEvent(event)
6. Things to note
Performance optimization
- Avoid placing too many items (more than thousands) in the scene.
- use
or
setCacheMode
Optimize rendering.
Coordinate conversion
usemapToScene()
andmapFromScene()
Convert coordinates between views, scenes, and items.
# Convert view coordinates (100, 200) to scene coordinatesscene_pos = (100, 200)
Memory management
Called when deleting graphic itemsremoveItem()
, avoid memory leaks.
(rect) del rect # Explicitly delete objects
7. Comprehensive case: Simple drawing tool
class DrawingScene(QGraphicsScene): def __init__(self): super().__init__() self.current_item = None def mousePressEvent(self, event): if () == : self.current_item = QGraphicsEllipseItem() self.current_item.setRect(().x(), ().y(), 0, 0) (self.current_item) def mouseMoveEvent(self, event): if self.current_item: start_pos = () current_pos = () self.current_item.setRect( start_pos.x(), start_pos.y(), current_pos.x() - start_pos.x(), current_pos.y() - start_pos.y() ) def mouseReleaseEvent(self, event): self.current_item = None #User Exampleapp = QApplication([]) scene = DrawingScene() view = QGraphicsView(scene) () ()
8. Summary
QGraphicsView and QGraphicsScene provide powerful support for complex graphics applications, enabling highly customized interactive interfaces by combining graphics items, processing events and optimizing rendering. During development, focus should be on coordinate systems, performance management and user interaction logic.
This is the article about the applicable scenario analysis of the QPropertyAnimation class of PyQt6/PySide6. For more related content of PyQt6 QPropertyAnimation class, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!