View Full Version : How to scale pixmap to fit
Splatify
7th February 2011, 16:56
Hi this is the code i currently have:
ui->imagebox->setPixmap(QPixmap("C:\\Picture2.png"))
where imagebox is a label.
How do i scale this to fit the label it is contained within?
Thanks for your time and trouble.
high_flyer
7th February 2011, 17:01
Have a look at the QPixmap doc:
QPixmap scaledToHeight ( int height, Qt::TransformationMode mode = Qt::FastTransformation ) const
QPixmap scaledToWidth ( int width, Qt::TransformationMode mode = Qt::FastTransformation ) const
JohannesMunk
7th February 2011, 18:42
I think you are looking for:
ui->imagebox->setScaledContents(true);
Joh
Splatify
8th February 2011, 06:44
Thanks Joh, your solution worked great.
beastian
21st April 2011, 16:28
Hi,
ui->imagebox->setScaledContents(true);
is great, however I need to scale the content of the label background picture just to the width (I've got always wider, then higher pictures and I do not want to make images to be deformed).
I've tried:
void SetImage( QLabel * pLabel, QImage * pImg )
{
pLabel->setBackgroundRole( QPalette::Base );
pLabel->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
pLabel->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
QPixmap qPixMap = QPixmap::fromImage( * pImg );
qPixMap.scaledToWidth( 400 ); // width of the label
pLabel->setPixmap( qPixMap );
}
but it doesn't work.
Can anybody suggest me how to do that.
I'll be grateful for an explanation, why the code above doesn't work.
Thank you a lot.
high_flyer
21st April 2011, 16:45
you didn't explain what "doesn't work" mean.
But I can prity much guess.
The docs for this method says:
Returns a scaled copy of the image.
So it does not alter the object calling the method, but RETURNS the result.
So you need to do something like:
qPixMap = qPixMap.scaledToWidth( 400 ); // width of the label
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.