PDA

View Full Version : Best way to implement "floating controls" for a QGraphicsView?



auryce74
13th June 2009, 16:54
I have a QGraphicsView subclass which lets the user examine the scene contents by zooming in/out, panning, etc.

I would now like to provide controls that are shown on top of the graphics view area (near the borders/corners mostly), and after reading a lot of Qt documentation I'm still not sure how one would accomplish this.

The kind of controls I'm thinking about are, for example:

a little scale showing how many pixels 1 logical unit is wide under the current zoom level. (should have interactive capabilities too, like more info when hovering, context menu with actions to choose the logical unit, etc.)
little magnifying class icons which initiate zooming in/out when clicked
informational icons popping up to give feedback on special things occuring while the user works with the view's contents
and more...


The important thing is that their geometry should not be affected by modifications to the viewport geometry, so they can't be QGraphicsItems inside the scene.

Also, I don't like the idea of drawing the controls in a reimplemented QGraphicsView::drawForeground function, because this works in scene coordinates, requiring all content (also pixmap icons and such) to be scaled to scene coordinates in order to draw them, only for the view to internally scale them back to screen coordinates. Also, implementing interactive capabilities would be cumbersome and require reproducing functionality already provided with QWidget (and also QGraphicsItem).

I have tried manually adding child widgets (QWidget!) to the graphics view (by calling QWidget::setParent on the widgets), but QGraphicsView seems to suppress them.

Is there a way to force it to show these wigdets?
That would be the best solution as far as I can see.

wysota
15th June 2009, 17:28
Probably the easiest way is to just add real widgets to the viewport and set their geometry upon changes to the viewport's geometry. Note that you need to add the widgets to the viewport and not the view.