[Qt-interest] how to paint monochrome QImage colorised(black=>arbitrary color, white=>transparent)?

Stephen Chu stephen at ju-ju.com
Sat May 8 17:57:59 CEST 2010


In article <hs3qg3$n4b$1 at eple.troll.no>,
 "Ross Bencina" <rossb-lists at audiomulch.com> wrote:

> Sorry to answer my own question. Google got me this:
> 
> QColor c = // color of image we want
> QImage image0 = QImage( 
> ":/ImageName.png" ).convertToFormat(QImage::Format_Indexed8);
> image0.setColor(0, c.rgb());
> image0.setColor(1, Qt::transparent);
> 
> (cache image 0)
> 
> painter.drawImage( x, y, image0 );
> 
> That works.
> 
> I'm not sure if this is the most efficient though. Is it worth caching the 
> image converted to the native screen color depth?
> 
> Ross.

If your image is a 1-bit bitmap, you can convert it to a QBitmap then 
set the background mode to transparent, set pen to the color you want. 
Then draw the pixmap:
   
   QBitmap theBitmap = QBitmap::fromImage(theImage);
   painter->setBackgroundMode(Qt::TransparentMode);
   painter->setPen(theColor);
   painter->drawPixmap(where, theBitmap);



More information about the Qt-interest-old mailing list