[PyKDE] Creating transparent QPixmaps

David Boddie david at boddie.org.uk
Thu May 25 18:39:04 BST 2006


On Wed May 24 14:06:12 MEST, Alexander Borghgraef wrote:

>   I was wondering, how can you create a QPixmap with a transparent
> background?

In Qt4, you can create a pixmap with the dimensions you need and fill it with
a transparent color:

  pixmap = QPixmap(width, height)
  pixmap.fill(QColor(0,0,0,0))

> I found some info on how to read images with an alpha channel, but what I
> want to do is to draw on an empty QPixmap using a QPainter (like you do
> with a buffer for instance).

You can now draw the pixmap with the painter. You can't create the initial
transparent pixmap using QPainter because painting using a transparent
brush will have no effect on the pixmap!

>  The goal is to be able to work in layers, like gimp does for instance: you
> have a pixmap image (image sequence stored in a QCanvasSprite actually, but
> I digress) on top of which you can place transparent layers on which the
> user or a routine (an optical flow routine e.g.) can draw. Any ideas?

If you're using Qt 3, this might be more involved. You might need to think
about using pixmaps with masks instead. So, you would create a bitmap as
well as a pixmap, fill the bitmap with a transparent color value, and use
it as the mask for the pixmap:

  pixmap = QPixmap(width, height)
  mask = QBitmap(width, height)
  mask.fill(Qt.color0)
  pixmap.setMask(mask)

When you update the pixmap, you also need to draw on the mask, and call
setMask() again to make sure that the pixmap uses the new mask information.

Hope this helps,

David




More information about the PyQt mailing list