[Qt-interest] QraphicsView fitInView

benswerts at telenet.be benswerts at telenet.be
Tue May 18 18:52:55 CEST 2010


>Hi,
>
>I am tryin to scale down a 1024x800 QImage so that it fits into a 100x100
>QGraphicsView widget.  I try to use the fitInView function but the image
>does not fit exactly in the QGraphicsView widget. Code-
>
>    QGraphicsScene* scene= new QGraphicsScene;
>
>    QPixmap np;
>    np = QPixmap::fromImage(*qImageData);
>    QGraphicsPixmapItem* qpitem = new QGraphicsPixmapItem;
>    qpitem->setPixmap(np);
>    scene->addItem(qpitem);
>    ui.graphicsView->setScene(scene);
>    ui.graphicsView->fitInView(scene->sceneRect(),Qt::IgnoreAspectRatio);
>    ui.graphicsView->show();
>
>With this code  the image shrinks completely and appears to be much smaller
>than the widget instead I want the image to fit perfectly into the 100x100
>widget (without scrolling)
>
>Thanks,
>Caius

Hi Caius,

A few pointers:
- Check whether the graphicsitem's size is the same as sceneRect() (I assume it is)
- The fitInView call can only work when the graphicsview has its final size. Before having it shown for the first time, it might not have this size yet. For that you'll need to call fitInView after the event loop was allowed to spin (QTimer::singleShot to a custom slot?)
- When you call fitInView once it will not resize completely correctly because it doesn't take into account the scrollbars of the graphicsview. Doing it twice fixes this. There's an old bug regarding this but I'd have to look up the number.

Hope it helps,


Ben






More information about the Qt-interest-old mailing list