Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Fixed transform overlays in QGraphicsView

In a Qt based application I'm developing I'm using a QGraphicsView to display sensor data in a 2D grid. On the side I'd like to show a legend/palette to relate the colours in the grid to values.

The user can zoom and pan the sensor data view, but understandably the palette should be stationary in the view. So placing the palette/legend in the sensor view scene requires some additional care: Applying the inverse user transformation.

However I'd rather have the palette/legend be implemented as kind of an (noninteractive) overlay layer with it's very own transformation. Is that somehow possible?

like image 544
datenwolf Avatar asked Feb 23 '23 21:02


1 Answers

Here's a really easy way to get a floating legend (or etc), I tested it under Qt 4.8.5 and Qt 5.2.1:

  1. Subclass QGraphicsView
  2. In your subclass, add an override method for the drawForeground(QPainter *, const QRectF &) method
  3. In your override method, call painter->resetTransform() before drawing anything
  4. Use the QPainter to draw graphics on top of your view, same as you would with any other QWidget

This works well -- in particular, with this approach you don't have to compute any inverse coordinate transformations or change the view's update mode.

like image 157
Jeremy Friesner Avatar answered Mar 04 '23 07:03

Jeremy Friesner