Scaling QPixmap to fit Label
Solved
General and Desktop
-
The following code is working, but the image is not scaled to fit the label.
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); QPixmap pix("C:/Programming/Qtsamples/Image_handling/IMG_6745.jpg"); ui->label->setPixmap (pix); } Dialog::~Dialog() { delete ui; } ```' How to make the image fit inside the label? (I tried several suggestions from the internet but they didn't work.) Thank you for your help.
-
adding to @Gerd
You can also doQPixmap p; // load pixmap // get label dimensions int w = label->width(); int h = label->height(); // set a scaled pixmap to a w x h window keeping its aspect ratio label->setPixmap(p.scaled(w,h,Qt::KeepAspectRatio);
If you need to scale it down to fit the are of the label.