Important: Please read the Qt Code of Conduct -

Shrink widget to fit it's new contents

  • I'm making a test program that displays an image and you can change that image. I have the image in QScrollArea and I want it to shrink when smaller image is chosen. I think the code explains it little better:
    QImage scaledImage = *image;
    // my function for resizing the image to fit the area
    resizeImage(&scaledImage, imageScrollArea->width(), imageScrollArea->height());


    // here i think i should somehow update the imageLabel

    // this is supposed to resize the scroll area to fit the new image

    // if i call sleep(5) here i can still see the old image for five seconds
    // which is why the previous two functions don't shink the scroll area

    I'm not sure if this is confusing so i'll explain little more. First I resize the new image to fit the scroll area. Then since the image ratio is kept there is some left over space in the scroll area and I want to shrink that away.

    This is what it now looks like after replacing the image (the scroll area is marked with a green background)

    I could do this with setFixedSize (because it doesn't care about the child widget which is the old image) but I can't use that since I want the image to be resizable by the user if he resizes the window.

Log in to reply