PDA

View Full Version : Maximize width of items in QGraphicsLinearLayout



haTem
24th May 2009, 09:08
Here is my setup. I have a custom QGraphicsItem (FooItem) containing a QWebView embedded in a QGraphicsProxyWidget.

I would like to arrange my FooItems in a vertical QGraphicsLinearLayout so that their width is always the width of the QGraphicsView (when resized).

What is the best way of accomplishing this? I have tried several different approaches, but have been unsuccessful.

Currently, I have a custom QGraphicsLayoutItem (FooLayoutItem) which sets its item as FooItem so that it can be managed by a layout. I have a QGraphicsWidget, which I resize to fit the QGV's width when it is resized. I assign my layout to that widget, and add my FooLayoutItems to it.

The items are arranged vertically, but the webviews are not stretched horizontally. I suspect the problem lies with FooLayoutItem's sizeHint(). How can I know the QGV's width in that function so that I can return the correct size?

Any assistance will be much appreciated.

wysota
24th May 2009, 09:53
sizeHint() is not as relevant here as the sizePolicy(). This is something you should concentrate on.

haTem
24th May 2009, 10:21
I have tried both the following (from inside the qgraphicsview):


mLayout->setMinimumWidth(width());
mLayout->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);

layoutItem->setMinimumWidth(width());
layoutItem->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);

I have also tried various other combinations such as QSizePolicy::Minimum and QSizePolicy::Expanding, etc. They are still not fitting the view's width horizontally. Any hints as to what I may be doing wrong?

Does Qt automatically handle the resizing from then on, or must I manually resize the QWebView from somewhere inside my FooItem?

Thank you for your help.

Edit: I have also tried setting the minimum width to viewport()->width() and scene()->width(). Neither have produced any effect.

wysota
24th May 2009, 10:36
Could you provide a compilable example reproducing the problem?

haTem
24th May 2009, 11:06
I have attached an example that shows the problem I am having. Thanks again for your help.

wysota
24th May 2009, 11:25
Your ChatItem should either be a QGraphicsWidget itself with a layout applied and the webview component inside that layout or you should use QGraphicsWidgetProxy directly, without additional items.

haTem
24th May 2009, 11:49
Thanks, I'll give that a try.

I'm planning on having other components (say, a Polygon) attached to the webview that might need to draw a small part of themselves on top of it, which is why I opted to not make ChatItem a QGraphicsWidget with a layout. If I added these polygons to the layout, I assume the layout would position them in such a way that no part of them are drawn on top of the webview.

Could I perhaps "lie" when reporting the polygon's boundingRect, such that the part that needs to draw on top of the webview is not included?

I suppose if that doesn't work, I can just increase the layout's spacing, and manually insert the polygons in the space between the layout's items, although that seems like an ugly hack :P.

wysota
24th May 2009, 11:53
If you decided to use QGraphicsWidgets, you should do that consistently - all your items should inherit QGraphicsWidget and they should use layouts. If, at some point you want to use a "non-widget" item, then you have to provide layouting functionality for its parent. I implemented something like a proxy item for regular graphics items that allows you to control a regular item inside the graphics-widgets world. You should do something similar.