Resizing pixmaps inside QGraphicsScene when the scene is resized

  • Hi there!
    I have a window which looks like this :
    What I want to do is a Game of 15 - I have to put multiple images in a grid inside this QGraphicsScene but the thing is, I want them to resize when I resize this window.
    The widgets inside the window resize automatically as they're inside a QGridLayout but I cant get the images inside the QGraphicsScene to resize.
    How do I do that?

    Thanks in advance!

  • Moderators

    you could zoom the graphicsview in the resizeEvent. The following code ensures that the image grid (bounding rect) is always visible inside the graphcisview.
    void MyGraphicsView::resizeEvent(QResizeEvent* event)

     QSize viewportSize = this->viewport().size();
     QSize imageGridSize = ...; //size of all images (bounding rect)
     qreal factor = viewportSize.width() / imageGridSize.width();
     if( viewportSize.height() < (imageGridSize * factor).height() )
        factor *= viewSize.height() / (imageSize * factor).height();
     QTransform transform = this->transform();
           transform.scale(factor, factor);


  • Zooming sounds like a good idea, shouldn't I zoom scene according to graphicsview size though?

  • Moderators

    this may work in some cases... but resizing to the viewport's size is the correct way since this is the visible area where your items are rendered onto.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.