Results 1 to 6 of 6

Thread: QLabel to display an image and preserve the aspect ratio

  1. #1
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QLabel to display an image and preserve the aspect ratio

    Hi

    This sounds very simple, but I can not figure it out...

    I need to display an image on a dialog as a part of the layout and the image should preserve the aspect ratio when the dialog size is changed and the QLabel geometry is changed accordingly. I tried to use QLabel for this, but it does not seem to be able to scale the image proportionally. Is there any other solution?

    Thanks
    Anton

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLabel to display an image and preserve the aspect ratio

    At the time of scaling the image. You can use these options:
    Qt Code:
    1. Qt::KeepAspectRatio
    2. Qt::KeepAspectRatioByExpanding
    To copy to clipboard, switch view to plain text mode 
    Whatever suits your requirement.

  3. #3
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QLabel to display an image and preserve the aspect ratio

    Quote Originally Posted by yogeshgokul View Post
    At the time of scaling the image. You can use these options:
    Qt Code:
    1. Qt::KeepAspectRatio
    2. Qt::KeepAspectRatioByExpanding
    To copy to clipboard, switch view to plain text mode 
    Whatever suits your requirement.
    So you propose to create a custom widget, re-implement paintEvent and draw a properly scaled image, right?

    Is there a way to do that via Designer without writing code - this is what I was thinking about.

  4. #4
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLabel to display an image and preserve the aspect ratio

    Quote Originally Posted by psih128 View Post
    So you propose to create a custom widget, re-implement paintEvent and draw a properly scaled image, right?
    Yes Subclass QLabel and scale the Pixmap properly in resizeEvent();

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLabel to display an image and preserve the aspect ratio

    You can probably also do it using "heightForWidth".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jun 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: QLabel to display an image and preserve the aspect ratio

    Hi! I was just searching how to solve this problem, and googling brings me to this post, so sorry for necroposting.

    Another solution is available when you know if the image is wider than taller or vice versa. For the first case - apply vertical layout, than add vertical spacer (below image), set image to scaled, set spacer sizeType = expanding, sizeHint.height = 65536 (some large value).

    This works because:
    <<
    scaledContents : bool
    This property holds whether the label will scale its contents to fill all available space.
    When enabled and the label shows a pixmap, it will scale the pixmap to fill the available space.
    >>

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.