Change the size of a QLabel inside the program
-
Greetings.
I wanted to know if you can change the size of a QLabel inside the program.
The problem is as follows:
I have a dialog box that shows, among many other widgets, a QLabel in which displays an image. I want the image then appears with the same ratio height / width actually have. For this, I set the minimum size of QLabel so that the ratio is respected. However, this does not guarantee that the image is shown with the minimum size of QLabel or maintaining the desired proportion, I keep getting images to appear larger sizes to minimum size without respecting the desired ratio, for example, images where height is greater than the width, are wider than high. Obviously, I understand that establish the minimum size of the label (which was what I did) does not generates the result I seek ... achieved only thing was that when the dialog is resized manually by the user to its minimum size, the picture looks as it should, but in other cases not... Do not know what else to do.
As I said I just want the label showing the retio height/width of the actual image, I do not mind not display a size slightly greater than the minimum size.
Another issue is that I would like to make the dialog appear with a height and width that were a percentage of the height and width of the parent widget respectively. However, the current size of the dialog box is determined by the size of QLabel, or rather, by the size at which the QLabel shows the image.
Thanks for any help and/or suggestions.
-
Just an idea:
Have you tried "QLabel::setScaledContents()":http://qt-project.org/doc/qt-5.1/qtwidgets/qlabel.html#scaledContents-prop ?What do you mean by "bigger than the minimum size"? That size is a minimum, so the QLabel should not hesitate to grow larger! Maybe you should try QLabel::setMaximumSize() instead?
Regarding the size of your dialog: If you set minimumSize() on your label, the dialog containing it might not be able to become any smaller than the label.
-
Hello,
If I understood you right you want that the ratio x=width/height of the image to be constant?Maybe setScaledContents() works but I'm not sure (it thinks it takes all the available space without keeping the aspect ratio). Maybe you should combine it with the right setAlignment() (Qt::AlignCenter, I think you want). What I thinks is the simplier solution in the case it doesn't work, is inheriting from QWidget and reimplementing resizeEvent(re) and paintEvent(). Inside the function you scale the image at :
@int w, h;
if(re->size().width()/re->size().height() > x) {
// Height is too small wrt width
h = re->size().height();
w = re->size().height()*x;
} else if(re->size().width()/re->size().height() < x) {
// Width is too small wrt height
h = re->size().width()/x;
w = re->size().width();
} else {
// The aspect ratio was kept
h = re->size().height();
w = re->size().width();
}@In this case you also will have to choose the right alignment. This is the hard way but you can do everything.
One warning : Forget to have your dialog resizing with the god deltas (x = deltawidth/deltaheight). This doesn't wok in all platforms because of the windows managers (as this is said in QWidget::sizeIncrement property). And when it does it is sometimes not really beautiful (windows flickers when resizing).
Regards,