Search code examples
c++imageqt4qlabelpixmap

How to save the image in a resized QLabel?


I have a QLabel that is loaded with a pixmap. I have it set so that when the window/QLlabel is resized, the image of the qlabel resizes as well. However, when I try to save the image and retrieve the pixmap, it is the original loaded. How would I go about retrieving the newly resized image from the QLabel and retrieving it as a QImage?


Solution

  • On the resize event of your label, Use

    Label->setPixmap(QPixmap::fromImage(YourImage).scaled(ui->Label->size(), Qt::IgnoreAspectRatio));
    

    For getting image as size of image use

    Label->pixmap().toImage();
    

    if this image is still not the same size as of label, Try

    Label->pixmap().toImage().scaled(Label->size(), Qt::IgnoreAspectRatio));