QLabel* label = new QLabel(); label->setPixmap(QPixmap("image.png")); label->setScaledContents(true);
QLabel* label = new QLabel(); QImage image("image.png"); label->setPixmap(QPixmap::fromImage(image.scaled(200,200,Qt::KeepAspectRatio))); label->setScaledContents(true);In this example, a QLabel is created and initialized with an image file "image.png". The image is first converted to a QPixmap using QPixmap::fromImage, and then scaled to a size of 200x200 pixels with the aspect ratio maintained using the scaled function. The setScaledContents function is called with a parameter value of true, indicating that the contents of the label (image in this case) should be scaled to fit the size of the label widget. The package library for this function is the Qt framework.