How should I use a QGraphicsScene with layouts and widgets
Asked Answered
T

2

10

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?

Tirade answered 24/6, 2009 at 14:26 Comment(0)
O
7

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);
Odontoid answered 29/7, 2009 at 2:14 Comment(0)
S
1

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.

Stephanstephana answered 24/6, 2009 at 18:32 Comment(3)
The biggest issue with QGraphicsScene is that it is NOT actually a widget. It's something like a collection of items (like rectangles, ellipses...) that are drawn automatically, can be zoomed etc. What you describe is the classic, plain old widget way, and I'll probably stick to that. I wanted to use the "shiny and new" functionality, but at the second thought, it doesn't look that shiny to me, except somebody proves me wrong :)Tirade
jpalecek: check the "clocks" widget demo @ qtsoftware.com/developer/pimp-my-widgets-developer-contest .. Nothing fancy on having a clock widget but implementation for that is done with QGraphics(Scene|Widget|View|Item).Antananarivo
@jpalecek: I didn't think a QGraphicsScene was a widget, which is why I said to inherit "one way or another". But you have a QGraphicsView (or something similar), right? You probably want to put your widgets over that, unless you want them transforming with your scene. I think in this case the plain old widget way is simpler and more straight-forward.Stephanstephana

© 2022 - 2024 — McMap. All rights reserved.