Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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:
    @
    image->load(filename);
    QImage scaledImage = *image;
    // my function for resizing the image to fit the area
    resizeImage(&scaledImage, imageScrollArea->width(), imageScrollArea->height());

    imageLabel->setPixmap(QPixmap::fromImage(scaledImage));
    imageLabel->adjustSize();

    // here i think i should somehow update the imageLabel

    // this is supposed to resize the scroll area to fit the new image
    imageScrollArea->setMinimumHeight(scaledImage.height());
    imageScrollArea->adjustSize();

    // 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)
    !https://i.imgur.com/Rm9iDOD.png(iamge)!

    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