PDA

View Full Version : Scaling window with image



danioto
18th October 2012, 12:31
Hi Community!

First of all, I want to say hallo, because it's my first post here :) I have a problem, which should be trivial, but I can't handle it by myself.

I have an application with two images as Pixmaps. I want them to be scaled when window is resized for example as 40% of width of the window. I've tried a resizeEvent function and there I've implemented resizing the images. It works, but only when window is making bigger, and when I want to make it smaller it's blocked because of the size of images... How can I disable the reducing-block-functionality?

Sorry for my poor English. If my problem is not clear for you, I'll give an example... :)

Greets,
Daniel

thomas@itest
18th October 2012, 14:02
Hi

I think that you can't shrink your image because of your widget minimum size ... and not the pixmap
try adding setMinimumSize(10,10) to see if it works.

but normally, if you set your QPixmap into a QLabel, you should not need to reimplement resizeEvent, just use QLabel::setScaledContents(true);

hope it helps ...

danioto
18th October 2012, 15:39
Ok, thanks thomas@itest. Could you tell me one more thing: How to keep the height and width in proportion? I can't find that option...

thomas@itest
18th October 2012, 16:19
me neither, i don't find this option, ...
so, I think that you were right : you must reimplement resizeEvent and scale the pixmap by hand using QPixmap::scale and Qt::AspectRatioMode

something like :



void myWidget::resizeEvent(QResizeEvent *event)
{
myLabel->setPixmap(myPixmap.scaled(event->size(),Qt::KeepAspectRatioByExpanding));
...
}

danioto
18th October 2012, 23:17
I think it won't work...

On one hand we set:

ImageLabel->setScaledContents(true);
and setting it we have an image label, which will expand to every free place not keeping the propotion. And then after resizeEvent:
1) Firstly - scaling the label image wont have any effect because we set ScaledContents earlier, so image wont keep proportion.
2) Secondly - we change the size of a label, so we couldn't shrink it...

How do you think? :)

Santosh Reddy
19th October 2012, 06:28
This should be useful

int QWidget::heightForWidth ( int w ) const

danioto
20th October 2012, 13:58
Hmm, in Reference Documentation is something like this:

void QSizePolicy::setHeightForWidth ( bool dependent )
Sets the flag determining whether the widget's preferred height depends on its width, to dependent.
But it doesn't work... Maybe I do something wrong...

QSizePolicy sizePolicy(QSizePolicy::Expanding,QSizePolicy::Exp anding);
sizePolicy.setHeightForWidth(true);

MyGrid=new QGridLayout();
setLayout(MyGrid);

Image1=new QImage(1000,1000,QImage::Format_RGB32);
Image2=new QImage(1000,1000,QImage::Format_RGB32);
Pixmap1=new QPixmap(QPixmap::fromImage(*Image1));
Pixmap2=new QPixmap(QPixmap::fromImage(*Image2));

ImageLabel1=new QLabel();
ImageLabel1->setScaledContents(true);
ImageLabel1->setSizePolicy(sizePolicy);
ImageLabel1->setPixmap(Pixmap1->scaled(300,300,Qt::KeepAspectRatio));

ImageLabel2=new QLabel();
ImageLabel2->setScaledContents(true);
ImageLabel1->setSizePolicy(sizePolicy);
ImageLabel2->setPixmap(Pixmap2->scaled(300,300,Qt::KeepAspectRatio));

MyGrid->addWidget(ImageLabel1,0,0);
MyGrid->addWidget(ImageLabel2,0,1);