Question

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?

Was it helpful?

Solution

I think you should implement your legend/palette overlay out of the QGraphics context, if you move (setPosition...) of this item to invert the user transformation you item will be re-indexed in the graphic tree any time you pan/zoom ...

Doing an overlay widget is definitely doable, but in this case since it's part of you graphic context I would recommend to implement your own QGraphicsView (wicth is a widget) re-implement the paintEvent to paint you palette/legend in default widget space (no transformation). Plus if your palette/legend is static you can render it once in a pixmap and simply repaint this buffer pixmap anytime the QGraphicsView needs an update (of course re-sizing will require the buffer to be updated ...).

If you do so no worry about the non-interacting part, you don't even have to forward events.

OTHER TIPS

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.

Maybe I misunderstood what you want to achieve, but you can mark QGraphicsItem wih QGraphicsItem::ItemIgnoresTransformations flag so you don't need to apply inverse transformations to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top