[Qt-interest] QGraphicsView fitinView question
Constantin Makshin
cmakshin at gmail.com
Thu May 27 08:07:32 CEST 2010
The description of QGraphicsView::fitInView() function says "rect must be inside the scene rect; otherwise, fitInView() cannot guarantee that the whole rect is visible.". In your case this function can't check if the passed rectangle fits into the scene because there's no scene assigned to the view.
When the image is smaller then QGraphicsView widget, zooming works just because of that — scene (image) is completely inside the view. But in other case the view doesn't know that the scene is larger than your widget and can't zoom correctly.
So try to put setScene() before fitInView().
On Thursday 27 May 2010 05:03:11 caius ligarius wrote:
> Hi,
>
> I am trying to fit a larger image (1024x768) in a smaller QGraphicsView
> widget (100x100) using fitinview but it doesn't seem to work. The image
> doesn't get scaled down instead the first 100x100 pixels of the image gets
> shown in the QGraphicsView widget. Code below-
>
> populateScene(QImage* qImage)
> {
> QPixmap np = fromImage(*qImage);
> graphicitemView->setPixmap(np);
> graphicssceneView->addItem(graphicitemView);
>
>
> ui.graphicsView->fitInView(graphicssceneView->sceneRect(),Qt::IgnoreAspectRatio);
> ui.graphicsView->setScene(graphicssceneView);
> ui.graphicsView->show();
> }
>
>
> Here -
> graphicitemView: is QGraphicPixmapItem
> graphicssceneView: is QGraphicsScene
> ui.graphicsView: is QGraphicsView
>
>
> How ever very strangely the reverse works quite well with the above code i.e
> - when I try to fit an image whose size is less than QGraphicsViews widget
> size.
>
> Can I use fitInView to scale down an image to fit in a smaller viewport?
>
> Thanks,
> Caius
More information about the Qt-interest-old
mailing list