Scaling window with image
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
Re: Scaling window with image
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 ...
Re: Scaling window with image
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...
Re: Scaling window with image
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 :
Code:
{
myLabel->setPixmap(myPixmap.scaled(event->size(),Qt::KeepAspectRatioByExpanding));
...
}
Re: Scaling window with image
I think it won't work...
On one hand we set:
Code:
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? :)
Re: Scaling window with image
This should be useful
Code:
int QWidget::heightForWidth ( int w
) const
Re: Scaling window with image
Hmm, in Reference Documentation is something like this:
Code:
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...
Code:
sizePolicy.setHeightForWidth(true);
setLayout(MyGrid);
ImageLabel1->setScaledContents(true);
ImageLabel1->setSizePolicy(sizePolicy);
ImageLabel1->setPixmap(Pixmap1->scaled(300,300,Qt::KeepAspectRatio));
ImageLabel2->setScaledContents(true);
ImageLabel1->setSizePolicy(sizePolicy);
ImageLabel2->setPixmap(Pixmap2->scaled(300,300,Qt::KeepAspectRatio));
MyGrid->addWidget(ImageLabel1,0,0);
MyGrid->addWidget(ImageLabel2,0,1);