Results 1 to 4 of 4

Thread: Dynamically scale QLabel with QImage to content while keeping aspect ratio

  1. #1
    Join Date
    Sep 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Dynamically scale QLabel with QImage to content while keeping aspect ratio

    I have a QImage that I want to show in the application (easiest way seems to be through a QLabel). However the application is meant to be able to use on different screen sizes, which means I can't just keep a rigid image size and call it a day. Using setScaledContent(true) is almost perfectly what I'm looking for, but unfortunately it doesn't care about aspect ratio.

    Any ideas?

    edit: Should probably have mentioned. I'm using QT 5.5.1 on Windows
    Last edited by Excludos; 2nd September 2016 at 12:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically scale QLabel with QImage to content while keeping aspect ratio

    You can scale the image before setting it on the label, usnig the label's size as the target size.

    QImage::scaled() has flags for things like keeping the aspect ratio

    Cheers,
    _

  3. #3
    Join Date
    Sep 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically scale QLabel with QImage to content while keeping aspect ratio

    That's only if you already know what size it needs to be. I figured if you setSizePolicy to MinimumExpanding in both directions, you can save the qlabel's width and height as int and use that in the QImage::scaled() method. That still requires you to set all your layouts correctly of course, or both widgets will just use half the layout each.

    But thanks for the answer!

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    69
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically scale QLabel with QImage to content while keeping aspect ratio

    Hi,

    You can catch the "resizeEvent" of you main window(or your QLabel) and there you can resize the QImage to fit the new QLabel size. Remeber to keep the original QImage
    Òscar Llarch i Galán

Similar Threads

  1. Replies: 3
    Last Post: 26th May 2015, 23:14
  2. Replies: 2
    Last Post: 7th December 2013, 19:02
  3. Printing and Aspect ratio
    By cpt4 in forum Qwt
    Replies: 9
    Last Post: 30th September 2011, 14:28
  4. Replies: 5
    Last Post: 11th June 2011, 15:29
  5. keeping aspect ratio while resizing
    By franco.amato in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2009, 20:12

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.