Results 1 to 5 of 5

Thread: setting QPixmap's alpha channel

  1. #1
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    7
    Qt products
    Qt4

    Default setting QPixmap's alpha channel

    Does there exist a better way to do it than this?

    Qt Code:
    1. void setAlphaChannel(QPixmap& pixmap, int const alpha)
    2. {
    3. QImage image(pixmap.toImage().convertToFormat(QImage::Format_ARGB32));
    4.  
    5. for (int x(0); x != image.width(); ++x)
    6. {
    7. for (int y(0); y != image.height(); ++y)
    8. {
    9. QColor color(image.pixel(x,y));
    10.  
    11. color.setAlpha(alpha);
    12.  
    13. image.setPixel(x, y, color.rgba());
    14. }
    15. }
    16.  
    17. pixmap = QPixmap::fromImage(image);
    18. }
    To copy to clipboard, switch view to plain text mode 

    I've read about the now-obsolete QPixmap method called setAlphaChannel(), but it's implementations are platform-dependent.

  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: setting QPixmap's alpha channel

    These are fairly equal (350ms over 1000 conversions) on my machine:
    Qt Code:
    1. QPixmap input("Lenna.png");
    2.  
    3. QImage image(input.size(), QImage::Format_ARGB32_Premultiplied);
    4. image.fill(Qt::transparent);
    5. QPainter p(&image);
    6. p.setOpacity(0.2);
    7. p.drawPixmap(0, 0, input);
    8. p.end();
    9.  
    10. QPixmap output = QPixmap::fromImage(image);
    11.  
    12. // or
    13.  
    14. QPixmap output(input.size());
    15. outut.fill(Qt::transparent);
    16. QPainter p(&output);
    17. p.setOpacity(0.2);
    18. p.drawPixmap(0, 0, input);
    19. p.end();
    To copy to clipboard, switch view to plain text mode 
    Your code comes in at around 8850 ms for the same 1000 conversions.

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

    ugluk (17th September 2012)

  4. #3
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    7
    Qt products
    Qt4

    Default Re: setting QPixmap's alpha channel

    Did you try method #1 without the fill? Perhaps this would work?

    Qt Code:
    1. QImage image(pixmap.size(), QImage::Format_ARGB32_Premultiplied);
    2.  
    3. {
    4. QPainter painter(&image);
    5.  
    6. painter.setCompositionMode(QPainter::CompositionMode_Source);
    7.  
    8. painter.setOpacity(.2);
    9. painter.drawPixmap(0, 0, pixmap);
    10. }
    11.  
    12. pixmap = QPixmap::fromImage(image);
    To copy to clipboard, switch view to plain text mode 

  5. #4
    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: setting QPixmap's alpha channel

    I did the pixmap version first: without the fill I was not getting an alpha channel in the PNG I saved from the pixmap version. I just left it in the image version, but it seems to work fine without and is faster still.

  6. #5
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    7
    Qt products
    Qt4

    Default Re: setting QPixmap's alpha channel

    I've found an example by Nokia, it is even more complicated

    http://www.developer.nokia.com/Commu...Pixmap_picture

    What puzzles me is the fill(Qt::transparent). Why is the fill() needed, if the entire transparent QPixmap or QImage is filled and the CompositionMode is QPainter::CompositionMode_Source?

Similar Threads

  1. QGLWidget renderPixmap and alpha channel
    By omega in forum Newbie
    Replies: 1
    Last Post: 12th September 2015, 01:55
  2. Replies: 1
    Last Post: 28th April 2011, 02:13
  3. alpha channel problems
    By codebehind in forum Qt Programming
    Replies: 2
    Last Post: 28th February 2010, 22:24
  4. SVG to alpha channel QPixmaps?
    By WinchellChung in forum Newbie
    Replies: 5
    Last Post: 24th August 2007, 21:07
  5. Replies: 2
    Last Post: 3rd November 2006, 22:20

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.