Important: Please read the Qt Code of Conduct -

[SOLVED]problem while resizing a QgraphicsScene

  • Hello to all,

    I am using a Canvas * scene which is made in that way in order to be zoomed to fit in a QGraphicsView. This scene, has been suclassed from a QGraphicsScene.I have noticed that when I am firstly loading an image of size (128,128) and then reload the scene with an image of size (214,256), then my first image is displayed properly to fit in the QGrapphicsView without scrollbars and then the second image is displayed in that way that scrollbars are displayed.
    When I load first an image (214,256) and then an image (128,128), the I get the first fitted without scrollbars ,but the second is displayed in the up-left corner of QGraphicsView, while the rest QGraphicsView is empty. How could I fit images of different sizes exactly in the size of QGraphicsView, without using any scrollbars?
    I think that every time I load my first image then the scene maintains the size of the image, and can't change it properly when an image of different size is loaded. Here is my code:

    @ ui->graphicsView_inputImage->setScene(scene=new Canvas());@

    QPixmap tmpmap (fileName, 0, Qt::AutoColor);
    if (!pixmapItem) {
    pixmapItem = scene->addPixmap (tmpmap);
    ui->graphicsView_inputImage ->height()/scene->height());
    } else {


  • QGraphicsScene is designed to grow automatically, to fit all items, but not shrink automatically. You need to update the sceneRect yourself after shrinking the scene. The easiest way should be to just reset the sceneRect after changing the pixmap, letting the scene calculate the new sceneRect itself:


    You could also set the sceneRect using the image size, but I don't think that's necessary.

  • Thanks a lot.The refresh of my scene seems to solve my problem

Log in to reply