PDA

View Full Version : QLabel, QPixmap and ScaledContents



mhbeyle
27th November 2009, 17:25
Hello.

I have a layout consists in one QLabel that contains a QPixmap. Because MainWindow is resizable, I want this QLabel resizable too. To obtain this I put the widget into a QScrollArea and later I have a Grid with this widget and another things like a group of labels, text boxes and so on. Finally I use setScaledContents(true) to resize the contens of the QLabel automatically.
But I have a expanded resize of QPixmap that doesn't preserve the aspect ratio.

How can I resize a QLabel (a QPixmap that resides into a QLabel) and preserve aspect ratio?

Thank you very much in advance.

mhbeyle
27th November 2009, 23:04
Well... I think that I have resolved my problem.

I have used paintEvent(QPaintEvent * ) to read the size of the Qpixmap and compare it with the QLabel container. Then, if the sizes differ, I use scaled() for the initial size of the QPixmap and Qt::KeepAspectRatio & Qt::SmoothTransformation to maintain the aspect ratio.

schnitzel
27th November 2009, 23:13
found this, but you already solved your problem just before I finished posting a response - would have deleted my post, but I'm not sure how to do that.

http://doc.trolltech.com/4.2/desktop-screenshot.html

mhbeyle
28th November 2009, 09:11
found this, but you already solved your problem just before I finished posting a response - would have deleted my post, but I'm not sure how to do that.

http://doc.trolltech.com/4.2/desktop-screenshot.html

This example demonstrates how to resolve the problem perfectly.
Screenshot Example uses QResizeEvent instead of QPaintEvent (more efficient in my opinion if the different sizes are verified earlier and the widget belongs to MainWindow) but I think that it is a very good example for all those that want to look at some piece of code.
I would not erase the post after all :) .