Results 1 to 3 of 3

Thread: Add letterbox to resized QPixmap

  1. #1
    Join Date
    Oct 2011
    Posts
    14
    Qt products
    Platforms
    Windows

    Default Add letterbox to resized QPixmap

    Hi,

    I want to resize an image with 4:3 aspect ratio and display it centered in a quadratic QGraphicsPixmapItem, thus having black bars above and below the image (letterbox). I know how to resize the image preserving its aspect ratio, but not how to center it on the PixmapItem and give the PixmapItem a black background or how to paste the resized image on a black quadratic image. Any suggestions?

    Thanks in advance,

    Jay-D

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Add letterbox to resized QPixmap

    I have no idea what a "quadratic QGraphicsPixmapItem" or "quadratic image" is, perhaps you meant square. Here is one way to get your letter (or pillar) boxed pixmap.
    Qt Code:
    1. QPixmap source("test.png"); // say 400x300
    2. QPixmap dest(512, 512);
    3. dest.fill(Qt::black);
    4. QPixmap resized = source.scaled(dest.size(), Qt::KeepAspectRatio);
    5. QPainter p(&dest);
    6. if (resized.width() < dest.width())
    7. p.drawPixmap( (dest.width() - resized.width())/2, 0, resized);
    8. else
    9. p.drawPixmap( 0, (dest.height() - resized.height())/2, resized);
    10. p.end();
    11. dest.save("output.png");
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    d_stranz (3rd April 2013)

  4. #3
    Join Date
    Oct 2011
    Posts
    14
    Qt products
    Platforms
    Windows

    Default Re: Add letterbox to resized QPixmap

    Wow, of course I meant "square", must have been the lack of sleep...
    Thanks a lot for your answer, I'm slowly starting to see the possibilities of QPainter.

Similar Threads

  1. Replies: 0
    Last Post: 2nd November 2011, 06:53
  2. Replies: 0
    Last Post: 1st November 2011, 12:30
  3. widget not resized within QDockWidget
    By pospiech in forum Qt Programming
    Replies: 2
    Last Post: 4th December 2008, 18:19
  4. QMdiSubWindow problem with resized window
    By estanisgeyer in forum Qt Programming
    Replies: 2
    Last Post: 7th January 2008, 14:39
  5. QTreeWidget - preventing columns from being resized.
    By sternocera in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2007, 16:11

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.