jpalecek
jpalecek

Reputation: 47762

How should I use a QGraphicsScene with layouts and widgets

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?

Upvotes: 10

Views: 16215

Answers (2)

Luper Rouch
Luper Rouch

Reputation: 9492

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);

Upvotes: 7

Caleb Huitt - cjhuitt
Caleb Huitt - cjhuitt

Reputation: 14941

If you think that QGraphicsScene (or whatever other widget you have) is appropriate for most of your display, use that. What we have done in the past for somewhat similar things is to make a custom widget that inherits (one way or another) from QWidget, and put the control widgets in a layout on top of that widget. This means that the whole widget is drawing whatever it is you want drawn, and the control widgets are on top of that, resizing as the whole widget is resized.

Alternatively, a couple of times we've had layouts that were just a bit too complicated for the layout widgets to easily handle. Rather than create a custom layout, we just positioned them with no layout, and moved them in code on the resize event. It works just as well.

Upvotes: 1

Related Questions