How should I use a QGraphicsScene with layouts and widgets

jpalecek picture jpalecek · Jun 24, 2009 · Viewed 14.5k times · Source

I'm creating some graphic data displaying widget in Qt4 and I was tempted to use the QGraphicsScene for it, create QGraphicsItems for the data items etc.

However, I wanted to add some layer of controls (eg. scrollbars, zoom+other buttons - I want to make it in a similar style as eg. Google Maps, that is, the data would be displayed all over the widget, and the buttons would be shown atop of them) to the widget. So I thought it might be feasible to add them to the scene (perhaps as a child of a QGraphicsGroupItem that would be shown over the data). But I want them to move & resize when I resize the whole widget, so I should use a QGraphicsLayout for managing them. But at this point, I discovered things are pretty complicated.

The problem is that when using QGraphicsLayout, the following constraints hold:

  1. Only a QGraphicsWidget can be managed by a layout
  2. QGraphicsLayout can only be used to manage children of a QGraphicsWidget

Which means that I would have to create my controls as QGraphicsWidgets, add a top level QGraphicsWidget to the data widget, and manage the size of this top level widget myself.

So I want to ask:

  1. Wouldn't a classic approach (ie. use plain old widgets for all controls, and use QGraphicsScene only for displaying the data) be more reasonable?

  2. Is there any advantage in using QGraphicsScene in this case (performance or simplicity...)?

  3. How should I use QGraphicsScene to exploit its strengths?

Answer

Luper Rouch picture Luper Rouch · Jul 29, 2009

Since Qt 4.4 you can embed classic widgets in a QGraphicsScene by using QGraphicsProxyWidget :

QWidget *widget = new QWidget;
QGraphicsScene scene;
QGraphicsProxyWidget *proxy = scene.addWidget(widget);