Results 1 to 12 of 12

Thread: Show Image on a QLabel

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

    Default Show Image on a QLabel

    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,
    Òscar Llarch i Galán

  2. #2
    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: Show Image on a QLabel

    Make sure the scaledContents property of the label is false.

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

    Default Re: Show Image on a QLabel

    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,
    Òscar Llarch i Galán

  4. #4
    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: Show Image on a QLabel

    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.

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

    Default Re: Show Image on a QLabel

    Hi,

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

    Thanks,
    Òscar Llarch i Galán

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

    Default Re: Show Image on a QLabel

    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,
    Òscar Llarch i Galán

  7. #7
    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: Show Image on a QLabel

    Yes, that's correct.

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

    Default Re: Show Image on a QLabel

    Hi,

    I'm having a little issue
    Qt Code:
    1. qImage = qImage.scaled(QSize(ui.myLabel->width(),1000),Qt::KeepAspectRatio,Qt::SmoothTransformation);
    To copy to clipboard, switch view to plain text mode 
    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,
    Òscar Llarch i Galán

  9. #9
    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: Show Image on a QLabel

    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.

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

    Default Re: Show Image on a QLabel

    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,
    Òscar Llarch i Galán

  11. #11
    Join Date
    Jun 2007
    Posts
    62
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Show Image on a QLabel

    I try to avoid the constant loading by using a QPixmapCache, but I do not know if it will solve your problem.

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

    Default Re: Show Image on a QLabel

    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.
    Òscar Llarch i Galán

Similar Threads

  1. QLabel size, for image display
    By C167 in forum Qt Programming
    Replies: 13
    Last Post: 25th October 2013, 16:09
  2. Replies: 13
    Last Post: 18th February 2008, 00:20
  3. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  4. Set the image at the center in QLabel?
    By vishal.chauhan in forum Qt Programming
    Replies: 5
    Last Post: 20th August 2007, 14:07
  5. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36

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.