Results 1 to 6 of 6

Thread: How to scale pixmap to fit

  1. #1
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question How to scale pixmap to fit

    Hi this is the code i currently have:


    Qt Code:
    1. ui->imagebox->setPixmap(QPixmap("C:\\Picture2.png"))
    To copy to clipboard, switch view to plain text mode 

    where imagebox is a label.

    How do i scale this to fit the label it is contained within?

    Thanks for your time and trouble.

  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: How to scale pixmap to fit

    Have a look at the QPixmap doc:

    QPixmap scaledToHeight ( int height, Qt::TransformationMode mode = Qt::FastTransformation ) const
    QPixmap 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
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to scale pixmap to fit

    I think you are looking for:
    Qt Code:
    1. ui->imagebox->setScaledContents(true);
    To copy to clipboard, switch view to plain text mode 
    Joh

  4. The following user says thank you to JohannesMunk for this useful post:

    Splatify (8th February 2011)

  5. #4
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to scale pixmap to fit

    Thanks Joh, your solution worked great.

  6. #5

    Default Re: How to scale pixmap to fit

    Hi,
    ui->imagebox->setScaledContents(true);
    is great, however I need to scale the content of the label background picture just to the width (I've got always wider, then higher pictures and I do not want to make images to be deformed).
    I've tried:
    Qt Code:
    1. void SetImage( QLabel * pLabel, QImage * pImg )
    2. {
    3. pLabel->setBackgroundRole( QPalette::Base );
    4. pLabel->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
    5.  
    6. pLabel->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
    7.  
    8. QPixmap qPixMap = QPixmap::fromImage( * pImg );
    9. qPixMap.scaledToWidth( 400 ); // width of the label
    10. pLabel->setPixmap( qPixMap );
    11. }
    To copy to clipboard, switch view to plain text mode 
    but it doesn't work.

    Can anybody suggest me how to do that.
    I'll be grateful for an explanation, why the code above doesn't work.

    Thank you a lot.

  7. #6
    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: How to scale pixmap to fit

    you didn't explain what "doesn't work" mean.
    But I can prity much guess.
    The docs for this method says:
    Returns a scaled copy of the image.
    So it does not alter the object calling the method, but RETURNS the result.
    So you need to do something like:
    Qt Code:
    1. qPixMap = qPixMap.scaledToWidth( 400 ); // width of the label
    To copy to clipboard, switch view to plain text mode 
    ==========================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.

  8. The following user says thank you to high_flyer for this useful post:

    ChrisOfBristol (8th April 2015)

Similar Threads

  1. Qwt Scale Draw
    By maxpayne in forum Qwt
    Replies: 2
    Last Post: 6th June 2012, 13:01
  2. QPainterPath and scale()
    By edwardc in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2010, 10:03
  3. qwt 5 log scale..
    By halberdier83 in forum Qwt
    Replies: 7
    Last Post: 12th November 2007, 07:21
  4. QGraphicsItem::scale() bug or not ?
    By Gopala Krishna in forum Qt Programming
    Replies: 7
    Last Post: 20th June 2007, 15:44
  5. How to copy pixmap and keep scale ratio?
    By Teerayoot in forum Qt Programming
    Replies: 2
    Last Post: 11th May 2007, 11:42

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.