PDA

View Full Version : QGraphicsView and QGraphicsScene resizing problem when using layouts



Sergex
12th August 2011, 18:22
Hello,

I'm having some problems with the implementation of these classes,
What I'm doing so far is I have a MainWindow and the window will have a QVBoxLayout. So basically this window will be divided by two vertically. I make two QWidgets and put each widget in the layout.

I want to set one image in the top half and another image in the Bottom half. So for this I created a QGraphicsView class that has a scene and an item derived from QGraphicsItem.

So I have in QMainWindow class:



...
topWidget = new QWidget;
bottomWidget = new QWidget;

grView = new QGraphicsView(topWidget);
scene = new QGraphicsScene(this);
scene->setScenRect(0,0,topWidget.width(), topWidget->height());
item = new GrItem(scene);

scene->addItem(item);
grView->setScene(scene);

...


Here I pass topWidget as the parent to the GraphicsView class so that it will contain the view widget. GrItem is a class derived from QGraphicsItem. I pass the scene as a parameter to be able to get the size of the sceneRect and in the paint method do :



...
painter->drawImage(sceneRect, myImage);


And after I set the layout:



mainLayout = QVBoxLayout;
mainLayout->addWidget(topWidget);
mainLAyout->addWidget(bottomWidget);

setLayout(mainLayout);



The problem I'm having is that only a portion of the image is getting drawn instead of the whole image getting drawn scaled to the size of the topWidget.

My question is where do I have to do the image sizing handling to display the whole image in this case in the whole top half of the mainwindow? I can't give setSceneRect a fixed QRecF because I also want to be able to resize the topWidget and bottomWidget and have the image always occupy the whole area of the widget that is containing the image. And since I am using layouts I don't set the size of the widgets beforehand. Also how do I properly set parent child relationships between GraphicsView, GraphicsScene and GraphicsItem?

Could someone please give me some guidance I would really appreciate it.

Thanks!

meazza
12th August 2011, 22:07
If your QGraphicsItem only paints an image to occupy the whole scene why arent you using QGraphicsScene::drawBackground()?

Also are you changing the size of the scene rect when you change the size of your QGraphicsView widget? This could be done in QGraphicsView::resizeEvent().

wysota
12th August 2011, 22:50
How about just using QLabel instead of those graphics views? Or even a custom widget?