Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Qt QBitmap

 

QBitmap is a Qt class used for masking/transparency in QPixmap. QBitmap is a class for containing monochrome (that is: each pixel is either on or off) images, instead of the colored Windows bitmaps.

 

The code below, adapted from Qt example 1: moving a sprite over a background in 2D, shows how to add transparency to a QPixmap.

 

#include <QGraphicsPixmapItem>

struct Sprite : public QGraphicsPixmapItem
{
  Sprite(
    const std::string& filename,
    const QColor& transparency_color = QColor(0,255,0)) //Lime green
  {
    QPixmap pixmap(filename.c_str());
    const QBitmap mask = pixmap.createMaskFromColor(transparency_color);
    pixmap.setMask(mask);
    this->setPixmap(pixmap);
  }
};

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict