Results 1 to 3 of 3

Thread: Display full scaled image on a QLabel

  1. #1
    Join Date
    Jan 2011
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Display full scaled image on a QLabel

    I have an Image I am trying to display in a label.

    I am using some code like the following:

    Qt Code:
    1. transformPixels(0,0,1,imheight,imwidth,1);//sets unsigned char** imageData
    2.  
    3. unsigned char* fullCharArray = new unsigned char[imheight * imwidth];
    4. for (int i = 0 ; i < imheight ; i++)
    5. for (int j = 0 ; j < imwidth ; j++)
    6. fullCharArray[(i*imwidth)+j] = imageData[i][j];
    7.  
    8. QImage *qi = new QImage(fullCharArray, imwidth, imheight, QImage::Format_RGB32);
    9.  
    10. ui->viewLabel->setPixmap(QPixmap::fromImage(*qi,Qt::AutoColor));
    To copy to clipboard, switch view to plain text mode 

    The imageData array contains a single unsigned char per pixel. It is a grayscale image, not full RGB32, but that format seems to be the closest I have gotten so far. the problem is, my label is only showing a portion of the image, which is large (larger than the screen). How can I make my image scale, keeping the proper proportions, so that it fits in the label? I dont want it to be stretched ot skewed or anything - just the full image, small enough to fit in the label.

    Thanks
    Last edited by high_flyer; 18th January 2011 at 21:11. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Display full scaled image on a QLabel

    How can I make my image scale, keeping the proper proportions, so that it fits in the label?
    Just use one of the following from QImage:
    QImage scaled ( const QSize & size, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const
    QImage scaled ( int width, int height, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const
    QImage scaledToHeight ( int height, Qt::TransformationMode mode = Qt::FastTransformation ) const
    QImage scaledToWidth ( int width, Qt::TransformationMode mode = Qt::FastTransformation ) const
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Display full scaled image on a QLabel

    Quote Originally Posted by high_flyer View Post
    Just use one of the following from QImage:
    I know this is an old thread, but that isn't adequate. If you scale the qimage, the label sizes itself - the original image is the same in as much as what the original post referred to.

Similar Threads

  1. display image in qlabel
    By aj2903 in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2014, 11:09
  2. QLabel size, for image display
    By C167 in forum Qt Programming
    Replies: 13
    Last Post: 25th October 2013, 16:09
  3. Replies: 5
    Last Post: 11th June 2011, 15:29
  4. Replies: 7
    Last Post: 13th August 2008, 18:27
  5. can Qlabel display a series of image one by one ?
    By jirach_gag in forum Qt Tools
    Replies: 3
    Last Post: 11th August 2008, 15: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.