PDA

View Full Version : Display full scaled image on a QLabel



dcole
18th January 2011, 21:21
I have an Image I am trying to display in a label.

I am using some code like the following:


transformPixels(0,0,1,imheight,imwidth,1);//sets unsigned char** imageData

unsigned char* fullCharArray = new unsigned char[imheight * imwidth];
for (int i = 0 ; i < imheight ; i++)
for (int j = 0 ; j < imwidth ; j++)
fullCharArray[(i*imwidth)+j] = imageData[i][j];

QImage *qi = new QImage(fullCharArray, imwidth, imheight, QImage::Format_RGB32);

ui->viewLabel->setPixmap(QPixmap::fromImage(*qi,Qt::AutoColor));


The imageData array contains a single unsigned char per pixel. It is a grayscale image, not full RGB32, but that format seems to be the closest I have gotten so far. the problem is, my label is only showing a portion of the image, which is large (larger than the screen). How can I make my image scale, keeping the proper proportions, so that it fits in the label? I dont want it to be stretched ot skewed or anything - just the full image, small enough to fit in the label.

Thanks

high_flyer
18th January 2011, 22:13
How can I make my image scale, keeping the proper proportions, so that it fits in the label?
Just use one of the following from QImage:


QImage scaled ( const QSize & size, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const
QImage scaled ( int width, int height, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const
QImage scaledToHeight ( int height, Qt::TransformationMode mode = Qt::FastTransformation ) const
QImage scaledToWidth ( int width, Qt::TransformationMode mode = Qt::FastTransformation ) const

astodolski
10th October 2013, 22:49
Just use one of the following from QImage:

I know this is an old thread, but that isn't adequate. If you scale the qimage, the label sizes itself - the original image is the same in as much as what the original post referred to.