PDA

View Full Version : Show Image on a QLabel



^NyAw^
11th April 2008, 17:15
Hi,

I have an application that contains a QLabel where I want to display an image.

There is a Layout that resizes the widgets. I want to display the image using the QLabel width and the height of the label have to be autoresized to don't stretch the image.
I have looked at imageviewer example but this is not what I need.

Thanks,

wysota
11th April 2008, 18:01
Make sure the scaledContents property of the label is false.

^NyAw^
14th April 2008, 09:26
Hi,

Yes, "scaledContents" is set to false.
The main application have a Horizontal Layout, that contains, on the left the QLabel to show the image and on right a set of widgets that have other layouts.
When the image is shown, the QLabel expands horizontally until the right layout don't let it to grow more.

Thanks,

wysota
14th April 2008, 10:46
Is the expansion caused by the image not fitting into smaller label? You might want to scale it accordingly or use QScrollArea if that's the case.

^NyAw^
14th April 2008, 10:58
Hi,

So, the solution is to get the QLabel size and resize the image to fit the QLabel size. Right?

Thanks,

^NyAw^
14th April 2008, 10:59
Hi,

So, the solution is to get the QLabel size and resize the image to fit the QLabel size. Right?

But, the width of the image will be the width of the QLabel, but the height of the QLabel have to be resized to the new image haight to not get image stretching. The QLabel on a layout let me resize the height?

Thanks,

wysota
14th April 2008, 10:59
Yes, that's correct.

^NyAw^
14th April 2008, 11:42
Hi,

I'm having a little issue


qImage = qImage.scaled(QSize(ui.myLabel->width(),1000),Qt::KeepAspectRatio,Qt::SmoothTransf ormation);

I want to keep aspect ratio so I set "height" of the scaled image to a big value(bigger that width).
Every time I show an image, the QLabel gains a little width(maybe 1 pixel).

I don't resize the QLabel height because it is autoresized when show the image(fitting to the image width and height).

Thanks,

wysota
14th April 2008, 11:51
You should always keep a verbatim copy of the image instead of resizing scaled down images. As for the size, you need to fix the size of the label if you rely on it when resizing the image. You needn't pass "1000" as the height - pass the height of the label. The size of the image probably increases because of a margin between the label's border and the pixmap.

^NyAw^
14th April 2008, 12:21
Hi,

Oh, so I have to get a copy of the width and height of the image resized when the QLabel is resized(when the application resizes), and resize the image with this values.



You should always keep a verbatim copy of the image instead of resizing scaled down images

Yes, but the application queries on a DB and so the image is reloaded every time.

Thanks,

WinchellChung
15th April 2008, 14:54
I try to avoid the constant loading by using a QPixmapCache, but I do not know if it will solve your problem.

^NyAw^
15th April 2008, 15:45
Hi,

Thanks, but I don't have this problem.
The problem was the size of the QLabel and the QImage and it has been solved.
The problem is not to load the image every time, because I don't need to speed up this part of code. It queries the name of the image to show on a DB an then load it on the QLabel. That's all.