Results 1 to 4 of 4

Thread: QLabel, QPixmap and ScaledContents

  1. #1
    Join Date
    Nov 2009
    Posts
    15

    Default QLabel, QPixmap and ScaledContents

    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.

  2. #2
    Join Date
    Nov 2009
    Posts
    15

    Default Re: QLabel, QPixmap and ScaledContents

    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.

  3. #3
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLabel, QPixmap and ScaledContents

    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
    Last edited by schnitzel; 27th November 2009 at 23:31. Reason: updated contents

  4. #4
    Join Date
    Nov 2009
    Posts
    15

    Default Re: QLabel, QPixmap and ScaledContents

    Quote Originally Posted by schnitzel View Post
    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 .

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.