To design a graphics object in Qt, you define a custom QGraphicsItem subclass (or build an QGraphicsObject when you need signals and properties), implement its boundingRect() and paint(), register the geometry with prepareGeometryChange() when it changes, and add the instance to a QGraphicsScene viewed through QGraphicsView. That core contract—bounds plus paint—is what makes the object render, hit-test, and update correctly. Everything else, from caching to event handling, hangs off those two methods. This guide walks through the whole design process in a workable order, using Qt 5/6 Widgets conventions, and flags the decisions that most often cause bugs.
Decide which base class your graphics object needs
Qt gives you three practical starting points, and picking the right one early saves rework. A plain QGraphicsItem is the lightest choice: it supports painting, bounding rectangles, collision detection, and mouse events, but it has no signals, slots, or Qt property system. It's the right base for decorative shapes, diagram nodes that never need to notify other objects, and high-count items where per-object overhead matters.
QGraphicsObject derives from both QGraphicsItem and QObject, so it adds signals, slots, and properties. Reach for it when other parts of your program need to react to changes in the item—for example, a node that emits positionChanged so a properties panel updates, or an item whose color is exposed as a Q_PROPERTY for animation. The cost is the QObject machinery per item, which is negligible for dozens of items and worth avoiding for tens of thousands.
QGraphicsWidget is a further specialization for items that participate in a layout and need a widget-like geometry (margins, size hints, focus handling). It suits composite panels inside a scene, not free-floating shapes. For most "design a graphics object" tasks, the honest answer is QGraphicsItem unless you can name the signal or property you need.
One more decision belongs here: whether the object should be several items or one. Qt composes naturally—a group item with child items that move together. Composite items are often easier to design than a single item that paints many disconnected pieces, because each child can have its own bounds, caching, and event handling.
Set up the scene and view before you write the item
Before implementing any object, get a minimal harness running so you can see results immediately. Create a QGraphicsScene, create a QGraphicsView, call view->setScene(scene), and show the view. Enable antialiasing on the view with setRenderHint(QPainter::Antialiasing); without it, rotated and curved shapes look jagged and you'll misjudge your paint code.
Two view settings matter for the design phase. setDragMode(QGraphicsView::RubberBandDrag) gives you selection testing for free, and setViewportUpdateMode(QGraphicsView::FullViewportUpdate) during early development removes an entire class of "my item leaves artifacts when it moves" confusion—switch back to the default minimal updates once the item's bounding rect is correct. Scaffold the scene with a known reference item (a simple rectangle at the origin) so you can judge scale and coordinates by eye.
The view's coordinate system is the scene's coordinate system by default, and the scene is unbounded. Decide early whether your design works in scene units that map to real-world measurements (millimeters on a plan, pixels of a diagram) or in arbitrary units. That choice affects every number in boundingRect() and every pen width you choose.
Define the geometry contract with boundingRect()
boundingRect() must return the item's outline in local coordinates, and it must be correct—Qt uses it for culling, collision, and update regions. If it's too small, painting outside it leaves artifacts and hit-testing misses; if it's too large, performance suffers and selection rectangles look wrong. A shape with a 5-unit pen that draws from (0,0) to (100,100) needs a bounding rectangle of roughly (-3, -3, 106, 106) if the pen is centered and you want the stroke fully inside, or you can disable pen adjustment and include the half-width yourself. Returning QRectF(0, 0, 100, 100) for a thicker pen is a classic source of clipped edges.
When geometry changes at runtime—a resize handle drags the item wider, a label's text grows—you must call prepareGeometryChange() before the stored geometry changes. This tells the scene to discard cached bounding-rect information and schedule the right repaints. Forgetting it produces exactly the symptom developers report most often: the item draws correctly in its new size but old pixels remain, or the new area isn't repainted at all.
Keep the bounding rect cheap. Don't compute text layouts or font metrics inside it on every call unless the value is cached and invalidated deliberately. Store the rectangle as a member when possible and return it.
Implement paint() so it stays inside the contract
paint() receives a QPainter already transformed into the item's local coordinates, plus the exposed rectangle and a style-option pointer you can usually ignore. The rule that governs everything: draw only inside boundingRect(). If you need to draw outside the bounds—a drop shadow, a glow, a tooltip-like callout—expand the bounding rect to include it.
A minimal skeletal implementation makes the structure concrete:
Notice what paint() does not do: it doesn't set the item's position, doesn't allocate, and doesn't mutate state other than the painter's. Treating paint() as a pure function of the item's fields is the discipline that keeps rendering predictable. Use setPen and setBrush on the painter rather than constructing a new QPainter — you should never create a QPainter inside paint().
For text, choose fonts and measure widths once, cache the layout, and redraw it. Qt's QGraphicsSimpleTextItem and QGraphicsTextItem exist precisely to avoid reimplementing this; consider composing them as children rather than painting text by hand if the label needs selection or HTML formatting.
Handle interaction through the item's event methods
Hit-testing defaults to the bounding rectangle, which is usually wrong for non-rectangular shapes. Override shape() to return a QPainterPath matching the visible geometry—a circle item should report a circular shape, an arrow a stroked path—so clicks near the corners of its bounding box don't count. shape() is also used by contains() and collision detection, so correcting it fixes selection, hover, and overlap behavior in one place.
To make an item movable, call setFlag(QGraphicsItem::ItemIsMovable); for selection, ItemIsSelectable; for focus and keyboard input, ItemIsFocusable. These flags replace most manual mouse handling. When you do override mousePressEvent(), call the base implementation first or last (depending on whether you want default behavior) and accept the event only if you handle it. A common bug is swallowing the press without accepting it, which breaks selection and dragging invisibly.
For resize handles, child items are cleaner than hit-testing corners yourself: add small QGraphicsRectItem handles as children, give each a cursor via setCursor(), and let each handle's mouseMoveEvent() adjust the parent's geometry through a prepareGeometryChange()-guarded setter. This composition pattern keeps each piece's logic small.
Keyboard interaction lives in keyPressEvent(), and focus must be on the item for it to fire. Set ItemIsFocusable, accept clicks to give focus, and draw a focus indicator in paint() when isSelected() or hasFocus() is true—users need to see which object is active.
Choose transforms, coordinates, and parenting deliberately
Item positions are expressed in parent coordinates; scene coordinates come from mapToScene()/mapFromScene(). Mixing the two is the second most frequent source of confusion after bounding rects. When you position a child, think in the parent's frame; when you compare items in different branches of the hierarchy, map both to the scene.
The ItemSendsGeometryChanges flag enables itemChange() notifications for position and transform changes, which is useful for snapping, constraint enforcement, or emitting signals from a QGraphicsObject. Enabling it has performance cost, so do it only on items that need it.
Transform origin matters. By default an item's transform applies around its local origin (0,0), so if your shape is drawn from (0,0) to (120,60), rotation pivots at the corner rather than the center. Either draw the shape centered around the origin or call setTransformOriginPoint() to choose the pivot deliberately. For scaling with handles, remember that pen widths scale with the transform unless you set QPen::setCosmetic(true), which keeps strokes visually constant—often exactly what diagram editors want.
Z-order is separate from parenting and controlled with setZValue(). Items with equal Z order stack by insertion. Keep your Z strategy simple: a small set of named layers (background, edges, nodes, overlays) as integer constants beats ad-hoc values scattered through the code.
Cache and tune only after the object is correct
setCacheMode(QGraphicsItem::DeviceCoordinateCache) is the standard optimization for items that move or animate without changing appearance: Qt renders the item once into a pixmap and blits it thereafter. It's a poor fit for items that repaint constantly or whose paint depends on the exposed rectangle in detail. ItemCoordinateCache suits static items at varying zoom levels but can look blurry when scaled up.
The other performance levers are setFlag(ItemUsesExtendedStyleOption), which gives paint() a precise exposed rectangle for partial repaint at some cost, and QGraphicsView::setOptimizationFlags on the view side. Don't reach for any of these until you've profiled a real scene; premature caching makes bugs harder to see because stale pixmaps mask bounding-rect errors. The measurement is straightforward: build a scene with a few thousand items, interact with it, and watch responsiveness. If it's fine, stop.
Verify the object with a short checklist
A custom item is working when each of these checks passes, and each failure maps to a specific fix:
1: Draw a test outline. Temporarily paint the bounding rect in a contrasting color. If strokes look clipped or selection boxes are offset, your boundingRect() is wrong. 2: Move the item by dragging. Stale pixels or trails mean boundingRect() is too small or updates aren't happening. 3: Resize programmatically. After changing geometry, artifacts indicate a missing prepareGeometryChange(). 4: Click the corners of a non-rectangular shape. Selection there means shape() needs overriding. 5: Rotate and zoom. Pen widths that balloon or vanish need cosmetic pens or a transform-origin decision. 6: Run a scene with hundreds of items. If interaction lags, revisit caching and avoid per-paint allocation before anything else.
Stop and reconsider the design when an item needs to coordinate tightly with many others, or when you find yourself reimplementing text editing, layout, or widget behavior inside paint(). At that point the honest move is composition with QGraphicsWidget or standard items, or stepping outside the Graphics View framework entirely for that part of the UI.
Where the framework stops fitting
Graphics View is designed for large numbers of lightweight 2D items with custom painting. It is not a document editor, a full layout engine, or a 3D renderer, and forcing it into those roles produces exactly the manual text, layout, and focus code you were trying to avoid. The reliable design instinct is to keep items small, keep paint() pure, keep geometry bookkeeping disciplined, and compose rather than centralize. Objects built that way stay debuggable as the scene grows—and that, more than any specific flag or cache mode, is what "designing a graphics object" actually means in Qt.